We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
publicstaticStringtwoStrings(Strings1,Strings2){//goal: determine if a string has a common substring//solution has o(n + m) time, o(n) space//use a set to store characters of the first stringSet<Character>string1CharSet=newHashSet<>();for(charc:s1.toCharArray()){string1CharSet.add(c);}//check if any character in the second string exists in the first setfor(charc:s2.toCharArray()){if(string1CharSet.contains(c)){return"YES";}}return"NO";//no common character was found}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Strings
You are viewing a single comment's thread. Return to all comments →
My Java solution: