Sort by

recency

|

1137 Discussions

|

  • + 0 comments

    This is my Python solution āœ… Explanation: 1. I first generate all unique character pairs from the input string. 2. For each pair, I build a filtered string that only includes those two characters. 3. I then check if the filtered string alternates properly (i.e., no repeated consecutive characters). 4. If it passes, I update the maximum length found so far. 5. In the end, return the longest valid alternating string length or 0 if none found.

    šŸ“Œ Note: Although this problem is tagged "Easy", it requires thoughtful implementation. I’d say it feels more like a Medium problem due to the character pairing and validation logic.

    Hope this helps others! 😊

    def alternate(s):
        # Write your code here
        def isConsecutive(st):
            if len(st) < 2:
                return True
            
            for i in range(1, len(st)):
                if st[i] == st[i-1]:
                    return True
            
            return False
        
        chars = list(set(s))
        candidates = []
        maxalter = float('-inf')
        
        for i in range(len(chars) - 1):
            for j in range(i + 1, len(chars)):
                candidates.append([chars[i], chars[j]])
        
        for cand in candidates:
            temp = ''
            for c in s:
                if c in cand:
                    temp += c
            if not isConsecutive(temp):
                maxalter = max(maxalter, len(temp))
        
        return maxalter if maxalter != float('-inf') else 0
    
  • + 0 comments

    Here is my c++ easy solution, explantion here : https://youtu.be/WAPtXpj-PSU

    int validSize(string s, char first, char second){
        string ans = "";
        for(int i = 0; i < s.size(); i++){
            if(s[i] == first || s[i] == second){
                if(ans.size() > 0 && ans[ans.size() - 1] == s[i]) return 0;
                else ans+=s[i];
            }
        }
        if(ans.size() < 2) return 0;
        return ans.size();
    }
    
    int alternate(string s) {
        int ans = 0;
        for(char i = 'a'; i < 'z'; i++){
            for(char j = i + 1; j <= 'z'; j++){
               int r = validSize(s, i, j);
               if(r > ans) ans = r;
            }
        }
        return ans;
    }
    
  • + 0 comments

    Here is my approach

    def is_alternated(t):
        for i in range(2, len(t)-1, 2):
            if t[i] != t[0] or t[i+1] != t[1]:
                return False
    
        if len(t) % 2 != 0:
            return t[0] == t[-1:]
    
        return True
    
    def alternate(s):
        _map = {}
        _set = set(s)
        _set_length = len(_set)
        if _set_length < 2:
            return 0
    
        c = list(combinations(_set, _set_length-2))
        for item in c:
            t = s
            for l in item:
                t = t.replace(l, "")
    
            if is_alternated(t):
                _map[t] = len(t)
    
        return max(_map.values()) if _map else 0
    
  • + 0 comments
    sub alternate {
        my $s = shift;
        my %s;
        my @s = grep { $s{$_}++ == 0 } split '', $s;
        my $max_length = 0;
        for(my $i = 0; $i < @s - 1; ++$i) {
            for(my $j = $i + 1; $j < @s; ++$j) {
                my $ns = $s =~ s/[^$s[$i]$s[$j]]//gr;
                if($ns =~ /^(.)(.)(\1\2)*\1?$/) {
                    if(length $ns > $max_length) {
                        $max_length = length $ns;
                    }
                }
            }
        }
        return $max_length;
    }
    
  • + 0 comments
    def alternate(s):
        max_length, chars = 0, set(s)
        pairs = combinations(chars, 2)
        for pair in pairs:
            candidate = s.translate({ord(char): '' for char in chars - set(pair)})
            remainder = len(candidate) % 2
            pair_equals_initial_pair = (
                candidate[i] == candidate[0] and candidate[i + 1] == candidate[1]
                for i in range(0, len(candidate) - remainder, 2)
            )
            last_char_equals_first_if_odd_length = candidate[-remainder] == candidate[0]
            if all(pair_equals_initial_pair) and last_char_equals_first_if_odd_length:
                max_length = max(max_length, len(candidate))
        return max_length