Sort by

recency

|

1130 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
    def lenIfValidResult(s, charA, charB):
        last_matching_char = None
        count = 0
        for char in s:
            if char == charA or char == charB:
                if last_matching_char == char:
                    return 0
                count += 1
                last_matching_char = char
        return count
    
    def alternate(s):    
        max_len = 0
        chars_in_str = set(s)
        if len(chars_in_str) <= 1:
            return 0
        for charA in chars_in_str:
            for charB in chars_in_str:
                if charA != charB:
                    result_len = lenIfValidResult(s, charA, charB)
                    max_len = max(max_len, result_len)
        return max_len
    
  • + 0 comments

    Some case tests must be wrong:

    Forinstance case test 4, I've found a list of size 25, with p and b. It's even easy to find the first ones. uyetup**p**elec**b**lwipdsqabzsvyfaezeqh**p**nalahnpkdbhzjglcuqfjnzpm**b**w**p**rel**b**ayyzovkhacgrglrdpmvaexkgertilnfooeazvulykxypsxicofnbyivkthovpjzhpohdhuebazlukfhaavfsssuupbyjqdxwwqlicbjirirspqhxomjdzswtsogugmbnslcalcfaxqmionsxdgpkotffycphsewyqvhqcwlufekxwoiudxjixchfqlavjwhaennkmfsdhigyeifnoskjbzgzggsmshdhzagpznkbahixqgrdnmlzogprctjggsujmqzqknvcuvdinesbwpirfasnvfjqceyrkknyvdritcfyowsgfrevzon

  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'alternate' function below.
    #
    # The function is expected to return an INTEGER.
    # The function accepts STRING s as parameter.
    #
    def consecutive_chars(s):
        for i in range(len(s) - 1):
            if s[i] == s[i + 1]:
                return 0
        return len(s)
        
    def alternate(s):
        # Write your code here
        tmp_list = list(set(s))
        if len(tmp_list) < 2:
            return 0
        tmp_list = sorted(tmp_list)
        length_list = []
        for i in range(len(tmp_list) - 1):
            for j in range(i+1, len(tmp_list)):
                new_s = [char for char in s if char == tmp_list[i] or char == tmp_list[j]]
                length_list.append(consecutive_chars(new_s))
        return max(length_list)
            
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        l = int(input().strip())
    
        s = input()
    
        result = alternate(s)
    
        fptr.write(str(result) + '\n')
    
        fptr.close()
    
  • + 0 comments

    My answer in Typescipt, simple, not minimized

    function alternate(s: string): number {
        // get a set of character in [s]
        let chars: string[] = Array.from(new Set<string>(s.split('')))
    
        // get everys pair of 2 character in [s]
        let pairs: [string, string, string?, number?][] = []
        for (let i = 0; i < chars.length; i++) {
            for (let j = i + 1; j < chars.length; j++) {
                pairs.push([chars[i], chars[j]])
            }
        }
    
        // check each pair, save info after remove other character except character in that pair
        for (let i = 0; i < pairs.length; i++) {
            let pair = pairs[i]
            let astr = s.split('').filter(c => pair.includes(c))
    
            pair.push(astr.join(''))
            pair.push(astr.every((c, j) => c != astr[j + 1]) ? astr.length : 0)
        }
    
        console.log(pairs)
    
        // return the largest valid result in [pairs]
        return Math.max(0, ...pairs.map(([a, b, str, result]) => result))
    }