[Hackerrank] – Two Strings Solution

    by nikoo28

    Question: Given two strings, determine if they share a common sub-string.

    Input: “a” and “art”

    Output: YES

    Problem Statement

    Let me try to simplify this problem statement first.

    You are given two strings, and you need to find out if they share a common sub-string or not. If you can find a common sub-string, output “YES”, else output “NO”. Note that you don’t have to find out what is the common sub-string.

    Another important aspect of this problem is that the sub-string can be as small as 1 character. A sub-string is basically any string that can be formed using one or more characters of the original string.

    Let us look at a sample test case:

    Example test case showing two strings with a common sub-string.
    Fig: Example of two strings with a common sub-string.

    So, if you are given with strings “CAT” and “RAT”, you can see that they share a common sub-string “AT”, hence in this case your answer should be “YES”

    Brute Force Method:

    One easy way to solve this problem would be:

    1. Find out all the sub-strings of first string.
    2. Find out all the sub-strings of second string.
    3. Once you have all the sub-strings, see if you can find any common sub-strings.
    4. Output “YES” or “NO” based upon your findings.

    Note that, this method works fine, but you will waste a lot of time in finding out all the valid sub-strings. So, we need to find a way to optimize it somehow.

    Efficient Method:

    There is a very big hint hidden in the problem statement itself. The problem says that the sub-string could be as small as one character. So, you do not need even need to find out all the sub-strings. If you can find a common character, that should be all.

    So let us assume you are given with two strings:

    str1 = “barbell”
    str2 = “trapper”

    An efficient way to solve this problem could be:

    1. Add all the characters of str1 to setA. So, setA would look like
      setA = < b, a, r, e, l >
    2. Add all the characters of str2 to setB. So, setB would become
      setB = < t, r, a, p, e >
    3. Take an intersection of both the sets. An intersection should give you the common characters.
    4. After the intersection, you will get the common characters as:
      < a, r, e >
    5. If you can find some characters after intersection, the result is “YES”, else “NO”.

    Code:

      public String twoStrings(String s1, String s2) {
    
        Set<Character> s1Set = new HashSet<>();
        Set<Character> s2Set = new HashSet<>();
    
        //creating the set of string1
        for(char c : s1.toCharArray()) {
          s1Set.add(c);
        }
        //creating the set of string2
        for(char c : s2.toCharArray()) {
          s2Set.add(c);
        }
    
        // store the set intersection in s1Set
        s1Set.retainAll(s2Set);
    
        if (!s1Set.isEmpty())
          return "YES";
    
        return "NO";
      }
    Code language: Java (java)

    Time Complexity: O(n)
    Space Complexity: O(1)
    The full solution and test cases are also available on Github.

    Video Explanation:

    YouTube player
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Between Two Sets Solution

    by nikoo28
    5 minutes read

    Question: You will be given two arrays of integers and asked to determine all integers between two sets that satisfy the following two conditions:– The elements of the first array are all factors of the integer being considered– The integer being considered is a factor of all elements of the second array Input:a = { 2, 6 }b = { 24, 36 } Output:2 I really want to simplify this really confusing problem statement first. The hardest part about this problem is to understand what is it actually saying. To …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Pairs Solution

    by nikoo28
    5 minutes read

    Question: Given an array of integers, find the number of pairs of array elements that have a difference equal to the target value. Example: Input:arr[ ] = {1, 2, 3, 4}k = 1 Output: 3 Problem Statement Let us try to simplify the problem statement first and understand the sample test case. You are given an array of unique integers which is in any random order. Along with the array, you are also given a target value k. If you pick up any 2 integers from the array, they would …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Missing Numbers Solution

    by nikoo28
    4 minutes read

    Question: You are required to find missing numbers that are left out while an artist transports numbers from one array to other. The output array should be sorted. Input:arr [ ] = {7, 2, 5, 3, 5, 3}brr [ ] = {7, 2, 5, 4, 6, 3, 5, 3} Output:Missing numbers: {4, 6} Problem Statement: Let me try to simplify the problem a little first. The problem description is quite verbose and we narrow down it to quite an extent. To summarize, the artist has an original array brr, and …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Equal Stacks Solution

    by nikoo28
    6 minutes read

    Question: Given 3 arrays, where each element represent the height of a cylinder. Find the maximum possible height of equal stacks by removing one or more cylinders from the original stack. Example: Input: n1[] = {3, 2, 1, 1, 1}n2[] = {4, 3, 2}n3[] = {1, 1, 4, 1} Output: 5 Problem Statement Let us try to simplify the problem statement first. The problem states that you are provided with 3 sets of arrays. Each element in the array represents the height of a cylinder. Now, all these cylinders are …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Left Rotation Solution

    by nikoo28
    7 minutes read

    Question: You are given an array of integers. After a left rotation of k times, find the resultant array. Example 1:Input: arr [ ] = {1, 2, 3, 4, 5}, size = 5, k = 2Output: {3, 4, 5, 1, 2} Example 2:Input: arr [ ] = {4, 8, 15, 16, 23, 42}, size = 6, k = 12Output: {4, 8, 15, 16, 23, 42} Terminology: To understand rotation of an array, you can assume that the array is kind of on an infinite conveyor belt, that keeps on looping. …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Theory

    Analysis of Algorithms

    by nikoo28
    3 minutes read

    There can be several aspects when you think about analysis of algorithms. You may be writing a large scale application where you have to decide between two choices. You may be trying to submit a solution to some online competition and you want a higher score, or probably you may just want to write fewer lines of code. Analysis during an Interview Let’s say you are appearing for an interview. The person who is interviewing you gives you a problem to solve. He or she is not just interested in …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More