Sunday, February 3, 2019

Two Strings - Hacker Rank Solution

Given two strings, determine if they share a common substring. A substring may be as small as one character.
For example, the words "a", "and", "art" share the common substring .
The words "be" and "cat" do not share a substring.
Function Description
Complete the function twoStrings in the editor below. It should return a string, either YES or NO based on whether the strings share a common substring.
twoStrings has the following parameter(s):
  • s1, s2: two strings to analyze .
Input Format
The first line contains a single integer , the number of test cases.
The following  pairs of lines are as follows:
  • The first line contains string .
  • The second line contains string .
Constraints
  •  and  consist of characters in the range ascii[a-z].
Output Format
For each pair of strings, return YES or NO.
Sample Input
2
hello
world
hi
world
Sample Output
YES
NO
Explanation
We have  pairs to check:
  1. . The substrings  and  are common to both strings.
  2.  and  share no common substrings.


Two Strings - Hacker Rank Solution

There are two concepts involved in solving this challenge:
  1. Understanding that a single character is a valid substring.
  2. Deducing that we only need to know that the two strings have a common substring — we don't need to know what that substring is.
Thus, the key to solving this challenge is determining whether or not the two strings share a common character because if they have a common character then they have a common substring of lengh 1.
To do this, we create two sets,  and , where each set contains the unique characters that appear in the string it's named after. Because sets don't store duplicate values, we know that the size of our sets will never exceed the  letters of the English alphabet. In addition, the small size of these sets makes finding the intersection very quick.
If the intersection of the two sets is empty, we print NO on a new line; if the intersection of the two sets is not empty, then we know that strings  and  share one or more common characters and we print YES on a new line.
# Complete the twoStrings function below.
def twoStrings(s1, s2):
    # create sets of unique characters
    # and test for intersection
    if set(s1)&set(s2):
        return "YES"
    else:
        return "NO"
Problem Tester's code:
import java.util.*;

public class Solution {
    static Set<Character> a;
    static Set<Character> b;

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        for(int i = 0; i < n; i++) {
            a = new HashSet<Character>();
            b = new HashSet<Character>();
            
            //creating the set of string1
            for(char c : scan.next().toCharArray()) {
                a.add(c);
            }
            //creating the set of string2
            for(char c : scan.next().toCharArray()) {
                b.add(c);
            }
            
            // store the set intersection in set 'a'
            a.retainAll(b);
            
            System.out.println( (a.isEmpty()) ? "NO" : "YES" );
        }
        scan.close();
    }
}

2 comments:

  1. what is time-complexity of this solution

    ReplyDelete
    Replies
    1. Let's say p = # of pairs, n = length of first string, and m = length of second string.
      This solution runs in O( p(n + m) ) because the outer loop runs for p iterations, and it has two sequential inner loops--the first one runs for n iterations, and the second one for m iterations.

      Delete

Powered by Blogger.