Sort by

recency

|

1136 Discussions

|

  • + 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
    
  • + 0 comments

    Here is my aproach of solving this problem

    function alternate(s) {
        let uniqueCharacters = new Set(s);
        let maxLength = 0;
    
        let uniqueCharsArr = [...uniqueCharacters];
    
        for (let i = 0; i < uniqueCharsArr.length; i++) {
            for (let j = i + 1; j < uniqueCharsArr.length; j++) {
                let firstChar = uniqueCharsArr[i];
                let secondChar = uniqueCharsArr[j];
                let validLength = checkValidString(s, firstChar, secondChar);
                maxLength = Math.max(maxLength, validLength);
            }
        }
    
        return maxLength;
    }
    
    function checkValidString(s, firstChar, secondChar) {
        let previousChar = '';
        let count = 0;
        for (let i = 0; i < s.length; i++) {
            if (s[i] === firstChar || s[i] === secondChar) {
                if (s[i] !== previousChar) {
                    count++;
                    previousChar = s[i];
                } else {
                    return 0; 
                }
            }
        }
    
    return count;
    

    }

    console.log(alternate("beabeefeab"));