Sort by

recency

|

2286 Discussions

|

  • + 0 comments

    Here is my simple c++ solution, video explanation here : https://youtu.be/xwLiYvExM6I

    string twoStrings(string s1, string s2) {
        map<char, int>mp;
        for(int i = 0; i < s1.size(); i++) mp[s1[i]] = 1;
        for(int i = 0; i < s2.size(); i++){
            if(mp[s2[i]]) return "YES";
        }
        return "NO";
    }
    
  • + 0 comments

    My Java solution:

    public static String twoStrings(String s1, String s2) {
            //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 string
            Set<Character> string1CharSet = new HashSet<>();
            for (char c : s1.toCharArray()) {
                string1CharSet.add(c);
            }
    
            //check if any character in the second string exists in the first set
            for (char c : s2.toCharArray()) {
                if (string1CharSet.contains(c)) {
                    return "YES";
                }
            }
            return "NO"; //no common character was found
        }
    
  • + 0 comments
    def twoStrings(s1, s2):
        return ('YES' if set(s1).intersection(set(s2)) else 'NO')
    
  • + 0 comments

    Approach:

    1. Count the freq of the 1st string.
    2. Traverse the freq mapp and check whether it contains the atleast 1 same key(character)
    3. if yes --> return "YES"
    4. else -->return "NO"
  • + 0 comments

    Here is my one line Python solution!

    def twoStrings(s1, s2):
        return "NO" if len(set(s1).intersection(set(s2))) == 0 else "YES"