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