Identifying the Odd One Out in a Series of Strings | Hackerrank

Author: neptune | 15th-Jun-2023
#Hackerrank #Problem Solving

In certain scenarios, we may encounter a series of strings where all but one string possess similar characteristics. Our goal is to identify the odd one out from the given series. To tackle this problem, we will develop an algorithm and provide its implementation in Python and Java. We will also analyse the complexity of the algorithm and conclude with its efficiency.


Problem Description: Given a series of strings, each containing only uppercase English letters, we need to determine the odd one out. The odd one out is the string that significantly differs from the others in terms of the calculated distances between consecutive characters.


Algorithm:

To solve this problem, we can follow the below steps:


1. Initialise an empty list called `distances` to store the calculated distances for each string in the series.

2. Initialise an empty string called `odd_one_out` to store the odd one out.

3. Iterate over each string `s` in the series.

   - For each string `s`, calculate the distances between consecutive characters and store them in a list called `dist`.

   - Append the tuple representation of `dist` to the `distances` list.

4. Iterate over the `distances` list.

   - Check if the count of a particular distance tuple is equal to 1.

   - If so, assign the corresponding string from the series to `odd_one_out`.

   - Break the loop.

5. Return the `odd_one_out` string.


Pseudo Code:


function findOdd(series):
    distances = []
    odd_one_out = ""

    for each string s in series:
        dist = []
        for i = 0 to length of s - 1:
            calculate the difference between the ASCII values of s[i+1] and s[i]
            append the difference to dist
        append the tuple representation of dist to distances

    for i = 0 to length of distances - 1:
        if count of distances[i] in distances is equal to 1:
            assign the corresponding string from series to odd_one_out
            break

    return odd_one_out



Python Implementation:


def findOdd(series):

    distances = []

    odd_one_out = ""


    for s in series:

        dist = []

        for i in range(len(s)-1):

            dist.append(ord(s[i+1]) - ord(s[i]))

        distances.append(tuple(dist))


    for i in range(len(distances)):

        if distances.count(distances[i]) == 1:

            odd_one_out = series[i]

            break


    return odd_one_out



Java Implementation:


import java.util.ArrayList;

import java.util.List;


public class OddOneOut {

    public static String findOdd(String[] series) {

        List<List<Integer>> distances = new ArrayList<>();

        String oddOneOut = "";


        for (String s : series) {

            List<Integer> dist = new ArrayList<>();

            for (int i = 0; i < s.length() - 1; i++) {

                dist.add((int) s.charAt(i + 1) - (int) s.charAt(i));

            }

            distances.add(dist);

        }


        for (int i = 0; i < distances.size(); i++) {

            if (distances.stream().filter(dist -> dist.equals(distances.get(i))).count() == 1) {

                oddOneOut = series[i];

                break;

            }

        }


        return oddOneOut;

    }


    public static void main(String[] args) {

        String[] series = {"ACB", "BDC", "CED", "DEF"};

        String oddOneOut = findOdd(series);

        System.out.println("Odd One Out: " + oddOneOut);

    }

}



Complexity Analysis

1. Let `n` be the number of strings in the series, and `m` be the length of each string.

2. Calculating the distances between consecutive characters for each string takes O(n * m) time.

3. Checking the count of each distance tuple also takes O(n * m) time.

4. Therefore, the overall time complexity of the algorithm is O(n * m).

5. The space complexity is O(n * m) to store the `distances` list.

Conclusion:

In this article, we discussed an algorithm to identify the odd one out from a series of strings. We provided implementations of the algorithm in Python and Java, along with the corresponding pseudo code. The algorithm calculates the distances between consecutive characters in each string and compares them to find the odd one out. We also performed a complexity analysis, which showed that the algorithm has a time complexity of O(n * m). By utilising this algorithm, we can efficiently solve the problem of identifying the odd one out in a series of strings.





Related Blogs
5. Solution of Hacker Rank Weather Observation Station 8.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Query the list of CITY names from STATION which have vowels (i.e., a, e, i, o, and u) as both their first and last characters. Your result cannot contain duplicates...

The Blunder | Hackerrank
Author: neptune | 21st-Nov-2022
#SQL #Hackerrank
Write a query calculating the amount of error (i.e.: average monthly salaries), and round it up to the next integer...

7.Solution of Hacker Rank The Report
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : generate a report containing three columns: Name, Grade and Mark. Ketty doesn't want the NAMES of those students who received a grade lower than 8...

4. Solution of Hacker Rank Weather Observation Station 6.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Query the list of CITY names starting with vowels (i.e., a, e, i, o, or u) from STATION. Your result cannot contain duplicates...

3. Solution of Hacker Rank Weather Observation Station 4.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Find the difference between the total number of CITY entries in the table and the number of distinct CITY entries in the table...

The PADS | Hackerrank
Author: neptune | 21st-Nov-2022
#SQL #Hackerrank
Problem Statement: Generate the following two result sets: 1. Query an alphabetically ordered list of all names in OCCUPATIONS, immediately followed by the first letter of each profession...

6. Solution of Hacker Rank Employee Salaries.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Query that prints a list of employee names for employees in Employee having a salary greater than $2000 per month and experience less than 10 months...

1. Basic SQL Select Query of Hacker Rank.
Author: neptune | 20th-Apr-2022
#SQL #Hackerrank
Problem Statement : Query all columns for all American cities in the CITY table with populations larger than 100000. The CountryCode for America is USA...

2. Solution of Hacker Rank Weather Observation Station 3.
Author: neptune | 23rd-Jan-2023
#SQL #Hackerrank
Problem Statement : Query a list of CITY names from STATION for cities that have an even ID number. Print the results in any order, but exclude duplicates from the answer...

Modified 0-1 knapsack problem | Frsco Play Hackerrank
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
An automobile mechanic wants to buy a set of spare parts from a manufacturing unit. Goal is to maximise the amount of money the mechanic can earn...

AngularJS - Know Tech Frameworks | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
Build an application that displays the framework details using AngularJS routing. We will build an application that displays the Tech Frontend and Backend frameworks...

Solving the Ice Cream Parlor Problem | Hackerrank
Author: neptune | 04th-Jun-2023
#Hackerrank #Problem Solving
Two friends like to pool their money and go to the ice cream parlour. They always choose two distinct flavours and they spend all of their money...

Backspace String Compare using R | Fresco Play
Author: neptune | 05th-Nov-2023
#Hackerrank #Problem Solving
The code implementation in both R and Python solves the "Backspace String Compare" problem using stack data structure...

Git - Recovering Discarded Changes
Author: neptune | 13th-Jul-2023
#Github #Problem Solving
We will follow a scenario where a developer is working on a web application and needs to recover a discarded commit to reintroduce a specific feature...

PySpark Milestone Black Friday Sales Data | Fresco Play Hackerrank
Author: neptune | 05th-Nov-2023
#Data Science #Hackerrank
Welcome to the Spark Challenge. You are provided with the Black Friday sales data and we as a big data developer needs to analyze and fetch the required data...

Problem of Merging Two Sorted Linked Lists
Author: neptune | 02nd-Jun-2023
#Algorithms #Problem Solving
Merging two sorted linked lists efficiently into a single, sorted list can be achieved using optimized algorithms...

Cassandra Products JSON | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
We'll go through a series of common tasks related to managing data in Cassandra...

Finding the Most Expensive Keyboard and USB Drive within a Budget | Hackerrank
Author: neptune | 05th-Jun-2023
#Hackerrank #Problem Solving
A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a give budget...

Python - Number Based Problem | Hackerrank
Author: neptune | 17th-Aug-2023
#Hackerrank #Problem Solving
Determine whether the number in descending order is a prime or not. If the number is a prime, then print "Sorted Number is a prime number," otherwise, print "Sorted Number is not a prime number."..

Join Operations in R | Fresco Play
Author: neptune | 29th-Oct-2023
#Hackerrank #Problem Solving
Perform various join operations in R using two data sets, 'flights' and 'weather'...

View More