Sort by

recency

|

1120 Discussions

|

  • + 0 comments

    Try with the script below on python and thank me later:)

    def alternate(s):
        valid_substrings = []
        for first, second in combinations(set(s), 2):
            pattern = "[^" + first + second + "]"
            substring = re.sub(pattern, '', s)
            if not re.search(r'([a-z])\1', substring):
                valid_substrings.append(substring)
        return max([len(i) for i in valid_substrings]) if valid_substrings else 0
    
  • + 1 comment

    This is 'easy'?

  • + 0 comments

    Haskell

    module Main where
    
    import Data.List (group, nub, tails)
    import Data.Maybe (mapMaybe)
    
    onlyKeep :: (Eq a) => [a] -> [a] -> [a]
    onlyKeep originals keepers = filter (`elem` keepers) originals
    
    allPairs :: [a] -> [[a]]
    allPairs xs = [[x, y] | (x : ys) <- tails xs, y <- ys]
    
    lengthIfValid :: (Eq a) => [a] -> Maybe Int
    lengthIfValid s =
        if and $ zipWith (/=) s (tail s)
            then Just $ length s
            else Nothing
    
    solve :: (Eq a) => [a] -> Int
    solve [] = 0
    solve s =
        let us = nub s
            pairs = allPairs us
            ls = mapMaybe (lengthIfValid . onlyKeep s) pairs
         in maximum $ 0 : ls
    
    main :: IO ()
    main = getLine >> getLine >>= print . solve
    
  • + 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

    for Python3 Platform

    import re
    import itertools
    
    def alternate(s):
        maxLen = 0
        unique_char = set(s)
        n = len(unique_char)
        pattern = re.compile(r'^(\w)(?!\1)(\w)(\1\2)*\1?$')
        
        if (n < 2):
            maxLen = 0
        else:
            for comb in itertools.combinations(unique_char, n-2):
                x = s
                
                for i in comb:
                    x = x.replace(i, "")
                
                if (pattern.match(x)):
                    maxLen = max(maxLen, len(x))
        
        return maxLen
    
    length = int(input())
    s = input()
    
    result = alternate(s)
    
    print(result)