Sort by

recency

|

1127 Discussions

|

  • + 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))
    }
    
  • + 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

    My C code, i think it was little bit difficult to find 😁😁😏

    int estAlternee(const char* s) {
        for (int i = 0; i < strlen(s) - 1; i++) {
            if (s[i] == s[i + 1]) {
                return 0;
            }
        }
        return 1;
    }
    
    
    int alternate(char* s) {
        if(strlen(s) == 1){
            return 0;
        }
        int maxLongueur = 0;
        int n = strlen(s);
    
    
        for (char a = 'a'; a <= 'z'; a++) {
            for (char b = a + 1; b <= 'z'; b++) {
              
                char temp[n + 1];
                int k = 0;
                for (int i = 0; i < n; i++) {
                    if (s[i] == a || s[i] == b) {
                        temp[k++] = s[i];
                    }
                }
                temp[k] = '\0';
    
     
                if (estAlternee(temp)) {
                    int longueur = strlen(temp);
                    if (longueur > maxLongueur) {
                        maxLongueur = longueur;
                    }
                }
            }
        }
    
        return maxLongueur;
    }
    
  • + 0 comments

    Java solution using a 1D array matrix

    class Result {
      public static final int NUM_LETTERS = 26;
      /*
       * 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) {
        // Edge case: if length <= 1, no alternations are possible
        if (s.length() <= 1) {
          System.out.println(0);
          return 0;
        }
        // Flattened array to represent the upper triangular portion of the matrix
        int[] count = new int[(NUM_LETTERS * (NUM_LETTERS - 1)) / 2];
        char[] lastSeen = new char[(NUM_LETTERS * (NUM_LETTERS - 1)) / 2];
        // Helper function to calculate the index in the flattened array
    
        // Process the string
        for (char c : s.toCharArray()) {
          int charIndex = c - 'a';
    
          // Update all pairs involving this character
          for (int i = 0; i < NUM_LETTERS; i++) {
            if (i == charIndex)
              continue;
    
            int index =
                charIndex < i ? getIndex(charIndex, i) : getIndex(i, charIndex);
    
            if (count[index] != -1) { // Only process if valid
              if (lastSeen[index] == c) {
                count[index] = -1; // Invalidate if repeating
              } else {
                lastSeen[index] = c; // Update last seen character
                count[index]++;
              }
            }
          }
        }
    
        // Find the maximum valid count
        int max = 0;
        for (int c : count) {
          max = Math.max(max, c);
        }
        return max;
      }
      private static int getIndex(int i, int j) {
        return i * NUM_LETTERS + j - ((i + 1) * (i + 2)) / 2;
      }
    }
    
  • + 0 comments

    def alternate(s):

    sum = 0
    pos = {ch:[i.start() for i in re.finditer(ch, s)] for ch in set(s)}
    for j in (x for x in combinations(pos, 2) if abs(len(pos[x[0]])-len(pos[x[1]]))<=1):
        s_pos = (j[0], j[1]) if pos[j[0][0]] < pos[j[1][0]] else (j[1], j[0])
        if len(pos[s_pos[0]]) < len(pos[s_pos[1]]):
            continue
        comb = [x for x in  (chain.from_iterable((n for n in zip_longest(pos[s_pos[0]], pos[s_pos[1]], fillvalue='_')))) if x != '_']
        if comb == sorted(comb):
            sum = max(sum, len(comb))
    
    return sum