Solving HackerRank Problem: Two Strings using C-Sharp

Problem

Given two strings, determine if they share a common substring. A substring may be as small as one character.....

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...

See the full description of the problem Two Strings

C# Solution

  • It is about finding just one character in first string which will also be present in second string.
  • Iterate the first string and create a set (hash set for O(1) ammortized searching) of all the english alphabet characters appearing in it.
  • Then, iterate the second string and check if any of the characters appearing is present in the character set of first string.
  • If found print YES else print NO.

Time Complexity: O(m + n) //m and n are length of the input strings respectively.

Space Complexity: O(1) 

Credit: @github.com/RyanFehr