Two Strings Hackerrank solution:Looking for Two Strings solution for Hackerrank problem? Get solution with source code and detailed explainer

Given two strings, determine if they share a common substring. A substring may be as small as one character.
Example
s1=’and’
s2=’art’

These share the common substring a.
s1=’be’
s2=’cat’

These do not share a substring.Go to problem statement

Explanation Video:

Youtube Channel partner: CodingCart
Mentor: Satyendra Jaiswal
Langauge: Python

Source Code:  for twoStrings function


# Complete the twoStrings function below.
def twoStrings(s1, s2):
    a=Counter(s1)
    flag=False
    for c in s2:
        if c in a.keys():
            flag=True
            break
    if flag:
        return 'YES'
    else:
        return 'NO'
coming soon…
coming soon…

Full Source Code: twoStrings HackerRank Solution


#!/bin/python3

import math
import os
import random
import re
import sys
from collections import Counter
# Complete the twoStrings function below.
def twoStrings(s1, s2):
    a=Counter(s1)
    flag=False
    for c in s2:
        if c in a.keys():
            flag=True
            break
    if flag:
        return 'YES'
    else:
        return 'NO'    
   
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    q = int(input())

    for q_itr in range(q):
        s1 = input()

        s2 = input()

        result = twoStrings(s1, s2)

        fptr.write(result + '\n')

    fptr.close()

coming soon…
coming soon…

Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our Youtube Channel.

This post is in partner with CodingCart.