Two Strings

Pratik Somwanshi
Hackerrank Algorithms
1 min readNov 23, 2018

Hello World!

Let’s understand the Two Strings problem on HackerRank. You may click on the title to read the problem statement. So let’s start.

The problem states that: Given two strings, determine if they share a common sub-string. If they do share a common substring, then we need to print “YES”, else “NO”.

I may remind you that it’s not necessary to derive all the substrings of both the given strings. The substring can be as small as a single character. Even if we have longer substring in common, we only need to find one character in common. So, on can just iterate through the letters in one string and find if the letter is in the second string or not.

There’s another approach that I used is to make sets of both the strings and then take a set intersection of the newly made sets.

If the intersection is null, they don't have anything in common, else they have at least one character in common.

Open for suggestions. The code for this problem in Python3 can be found here.

Happy coding…

--

--