Two Characters

Sort by

recency

|

28 Discussions

|

  • + 0 comments

    Python3:

    def alternate(s):
        mem = Counter(s)
        cb = filter(lambda p: abs(mem[p[0]] - mem[p[1]]) <= 1, combinations(mem.keys(), 2))
        cb = sorted(cb, key= lambda p: mem[p[0]] + mem[p[1]], reverse= True)
        
        for c1, c2 in cb:
            ss = [c for c in s if c in (c1, c2)]
            if all(ss[i] != ss[i+1] for i in range(len(ss) - 1)):
                return len(ss)
        return 0
    
  • + 0 comments

    Python

    # uses letter1 and letter2 as the combo letters
    # returns length of longest alternating str with them, 0 if not possible
    def getcombolength(s, letter1, letter2):
        count = 0
        toggleletter = '?'
        for letter in s:
            if letter == letter1 or letter == letter2:
                if letter == toggleletter:
                    return 0
                count += 1
                toggleletter = letter
        return count
    
    def alternate(s):
        if len(s) <= 1:
            return 0
        letters = 'abcdefghijklmnopqrstuvwxyz'
        maxcombo = 0
        for (letter1, letter2) in [(letters[j], letters[k]) for j in range(26) for k in range(j+1, 26)]:
            combo = getcombolength(s, letter1, letter2)
            maxcombo = max(combo, maxcombo)
        return maxcombo
    
  • + 0 comments

    Java 33/33 test cases, O(n^2)

    public static int alternate(String s) {
            if (s.length() == 1) {
                return 0;
            }
            
            int maxAlternateLength = 0;
            
            for (char c1 = 'a'; c1 <= 'z'; c1++) {
                for (char c2 = (char) (c1 + 1); c2 <= 'z'; c2++) {
                    int alternateLength = 0;
                    char prev = '\0';
                    boolean isValid = true;
                    
                    for (char ch : s.toCharArray()) {
                        if (ch == c1 || ch == c2) {
                            if (ch == prev) {
                                isValid = false;
                                break;
                            }
                            alternateLength++;
                            prev = ch;
                        }
                    }
                    
                    if (isValid) {
                        maxAlternateLength = Math.max(maxAlternateLength, alternateLength);
                    }
                }
            }
            
            return maxAlternateLength;
        }
    
  • + 0 comments
    class Result {
    
        /*
         * Complete the 'alternate' function below.
         *
         * The function is expected to return an INTEGER.
         * The function accepts STRING s as parameter.
         */
    
        public static int alternate(String s) {
        // Write your code here
        int[][] countMap = new int[26][26];
        char[][] lastChar = new char[26][26];
        
        int ans = 0;
        
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            int index = c - 'a' ;
            for (int j = 0; j < 26; j++){
                if (countMap[index][j] == -1 ){continue;}
                if (lastChar[index][j] == c){
                    countMap[index][j] = -1;
                    countMap[j][index] = -1;
                }
                else{
                    lastChar[index][j] = c;
                    lastChar[j][index] = c;
                    countMap[index][j] += 1;
                    countMap[j][index] += 1;
                }    
            }
        }
        for (int i=0 ; i<26 ; i++){
            for (int j=0 ; j < 26 ; j++) {
                if (i==j){continue;}
                ans = Math.max(countMap[i][j], ans);
            }
        }
        if (s.length() == 1){return 0;}
    
        return ans;
        
        
        }
    }
    
  • + 0 comments

    C#

    public static int alternate(string s)
        {
            int maxLength = 0;
            HashSet<char> uset = new HashSet<char>(s.ToCharArray());
            char[] chars = uset.ToArray();
            for (int i = 0; i < chars.Length; i++)
                for (int j = i+1; j < chars.Length; j++) {
                    int len = GetDuoCharsLength(s, chars[i], chars[j]);
                    if (len > maxLength) maxLength = len;
                }
                
            return maxLength;
        }
        
        private static int GetDuoCharsLength(string s, char ch1, char ch2) {
            if (s.IndexOf(ch1) > s.IndexOf(ch2)) {
                char temp = ch1;
                ch1 = ch2;
                ch2 = temp;
            }
            int len = 0;
            bool turn = true;
            foreach (char ch in s) {
                if (ch != ch1 && ch != ch2) continue;
                if ((turn && ch == ch1) || (!turn && ch == ch2)) {
                    turn = !turn;
                    len++;
                } else {
                    return 0;
                }
            }
            return len;
            
        }