Sort by

recency

|

2280 Discussions

|

  • + 0 comments
    def twoStrings(s1, s2):
        x=list(set(s1))
        y=list(set(s2))
        for i in x:
            for j in y:
                if i==j:
                    return 'YES'
        return 'NO'
    

    or

    def twoStrings(s1, s2):
        set1 = set(s1)
        set2 = set(s2)
        return "YES" if set1 & set2 else "NO"
    

    or

    def twoStrings(s1, s2):
        def bitmask(s):
            bitmask = 0
            for char in s:
                bitmask |= (1 << (ord(char) - ord('a')))
            return bitmask
        bm1 = bitmask(s1)
        bm2 = bitmask(s2)
        return "YES" if bm1 & bm2 else "NO"
    
  • + 0 comments

    My answer with Typescript, I am a bit surprised by this question, is it too simple?

    function twoStrings(s1: string, s2: string): string {
        for (let c of s1) if (s2.includes(c)) return 'YES'
        return 'NO'
    }
    
  • + 0 comments

    Perl:

    sub twoStrings {
        my @str1 = split("", shift);
        my @str2 = split("", shift);
        my %h;
        for (my $k = 0; $k <= scalar(@str1) - 1; $k++) {
            $h{$str1[$k]} = 0;
        }
        for (my $i = 0; $i <= scalar(@str2) - 1; $i++) {
            if (exists($h{$str2[$i]})) {
                return "YES";
            }
            
        }
        return "NO";
    }
    
  • + 0 comments

    Java:

    public static String twoStrings(String s1, String s2) {
      boolean[] charPresence = new boolean[26]; // a to z
    
      // Mark characters present in the array
      for (char c : s1.toCharArray()) {
        charPresence[c - 'a'] = true;
      }
    
      // Check if any character in the second string exists in the array
      for (char c : s2.toCharArray()) {
        if (charPresence[c - 'a']) {
          return "YES"; // Common character found
        }
      }
    
      return "NO"; // No common character found
    }
    
  • + 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";
    }