Sort by

recency

|

2293 Discussions

|

  • + 0 comments

    Is there any O(n) solution to solve this. In c++.

  • + 0 comments
    public static String twoStrings(String s1, String s2) {
        String maxstring = s1.length() > s2.length() ? s1 : s2;
        String smallstring = s1.length() <= s2.length() ? s1 : s2;
        Set<String> maxset = Arrays.stream(maxstring.split("")).collect(Collectors.toSet());
        Set<String> smallset = Arrays.stream(smallstring.split("")).collect(Collectors.toSet());
        int smallsetsize = smallset.size();
        smallset.removeAll(maxset);
        return smallsetsize == smallset.size() ? "NO" : "YES";
    }
    
  • + 0 comments

    python O(n + m) time O(n) space:

    def twoStrings(s1, s2):
        strset = set(s1)
        
        for c in s2:
            if c in strset:
                return "YES"
        
        return "NO"
    
  • + 0 comments

    Python3

    def twoStrings(s1, s2):
        int_set = set(s1).intersection(set(s2))
        return 'YES' if len(int_set)>0 else 'NO'
    
  • + 0 comments

    Here is my simple C++ solution:

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