Sort by

recency

|

1124 Discussions

|

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

    Java solution:

    public static boolean isValidateString(String s){
            int n = s.length(); 
            for(int i = 1; i < n; i++){
                char current = s.charAt(i);
                if(current == s.charAt(i-1))
                    return false;    
                }
            return true; 
           }
    			 
    			 public static int alternate(String s) {
            int n = s.length();
            Set<String> set = new HashSet<String>( Arrays.asList(s.split("") )  );
            for(int i = 1; i < n; i++){
                char current = s.charAt(i);
                if(current == s.charAt(i-1)){
                    set.remove(String.valueOf(current) );
                    }
                }
            int m = set.size();
            String[]elements = set.toArray(new String[m]);
            int maxLen = 0;
            
            for(int i = 0; i < m-1; i++)
                for(int j = i+1; j < m; j++){
                    char c1 = elements[i].charAt(0);
                    char c2 = elements[j].charAt(0);
                    String result = "";
                    for(int k = 0; k < n; k++){
                        char curr = s.charAt(k);
                        if(curr == c1 || curr == c2)
                          result += curr;
                        }
                    if(isValidateString(result))
                        maxLen = Math.max(maxLen, result.length());
                    }
            return maxLen;
    
            }
    			 
    			 
    
  • + 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

    C#

    public static int alternate(string s) { // Step 1: Get all unique characters in the string HashSet uniqueChars = new HashSet(s);

        int maxLength = 0;
    
        // Step 2: Try every pair of unique characters
        foreach (char char1 in uniqueChars)
        {
            foreach (char char2 in uniqueChars)
            {
                if (char1 != char2)
                {
                    int currentLength = 0;
                    char lastChar = '\0';
                    bool isValid = true;
    
                    // Step 3: Loop through the string
                    foreach (char c in s)
                    {
                        if (c == char1 || c == char2)
                        {
                            // If two consecutive characters are the same, it's invalid
                            if (c == lastChar)
                            {
                                isValid = false;
                                break;
                            }
                            lastChar = c;
                            currentLength++;
                        }
                    }
    
                    // If the string is valid, update maxLength
                    if (isValid)
                    {
                        maxLength = Math.Max(maxLength, currentLength);
                    }
                }
            }
        }
    
        return maxLength;
    }
    
  • + 0 comments

    Here is my Golang solution

    func alternate(s string) int32 {
        // Write your code here
        possibilityMap := make(map[string]bool)
        possibilityComb := [][]string{}
        
        for i := 0; i <= len(s) -1; i++{
            for j:= 0; j <= len(s)-1; j++{
                if s[i] == s[j]{continue}
                
                current := string(s[i])+string(s[j])
                if !possibilityMap[current]{
                    alter := string(s[j])+string(s[i])
                    if !possibilityMap[alter]{
                        possibilityMap[current] = true
                        temp := []string{}
                        temp = append(temp, string(s[i]))
                        temp = append(temp, string(s[j]))
                        possibilityComb  = append(possibilityComb, temp)
                    }
                }
            }
        }
        
        counter := 0
        isRepeat := false
        for i := 0; i < len(s)-1; i++{
                if i+1 >= len(s){break}
                if s[i] == s[i+1]{isRepeat = true}
        }
        if !isRepeat && len(possibilityMap) == 2 {return int32(len(s))}
            
        
        for _, v := range(possibilityComb){
            newS := strings.ReplaceAll(s, string(v[0]), "")
            newS = strings.ReplaceAll(newS, string(v[1]), "")
                
            newSS := s
            for _, v2 := range(newS){
                newSS = strings.ReplaceAll(newSS, string(v2), "")
            }
            
            isRepeat := false
            for i := 0; i < len(newSS)-1; i++{
                if i+1 >= len(newSS){break}
                if newSS[i] == newSS[i+1]{isRepeat = true}
            }
            if !isRepeat && len(newSS) > counter {
                counter = len(newSS)
            }
        }
        
        return int32(counter)
    }