Hackerrank - Strings - Two Strings

Given two strings,  and , determine if they share a common substring.
Input Format
The first line contains a single integer, , denoting the number of  pairs you must check.
Each pair is defined over two lines:
  1. The first line contains string .
  2. The second line contains string .
Constraints
  •  and  consist of lowercase English letters only.
Output Format
For each  pair of strings, print YES on a new line if the two strings share a common substring; if no such common substring exists, print NO on a new line.
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  and , so we print YES on a new line.
  2. . Because  and  have no common substrings, we print NO on a new line.
Solution :
# Enter your code here. Read input from STDIN. Print output to STDOUT
itt = int(raw_input())

for k in range(itt):
    string_1 = list(raw_input())
    string_2 = list(raw_input())
    dic = { }
    result = False
    for l in string_1:
        if l not in dic:
            dic[l] = 1
        else:
            dic[l] += 1

    for k in string_2:
        if k in dic:
            result = True
            break

    if result:
        print "YES"
    else:
        print "NO"

Comments

Best Programming language for machine learning.

Popular posts from this blog

Hackerrank - Implementation - Picking Numbers

Hackerrank - Dynamic Programming - The Coin Change Problem