Sort by

recency

|

1144 Discussions

|

  • + 0 comments

    Just realized that i invested like 1 hour doing a algorythm that recursively check every possibility with any numbers of char set above 2. Almost passed every test, but the was a timeout issue. Folks out there pay attention to the last sentence... TWO CHARACTERS, not 3, not 4, not N, just maximum of two:

    Given a string, remove characters until the string is made up of any two alternating characters. When you choose a character to remove, all instances of that character must be removed. Determine the longest string possible that contains just two alternating letters.

  • + 0 comments

    Hint, there is no need to be efficient here, so just try what seems eassiest:

    int alternating(const string& str, char a, char b) {
        char prev = 0;
        int len = 0;
        for (char c : str) {
            if (c != a && c != b) continue; 
            if (c == prev) return -1;       
            prev = c;
            len++;
        }
        return len;
    }
    
    int alternate(string s) {
        set<char> str_set(s.begin(), s.end());
        vector<char> uniques;
        
        copy(str_set.begin(), str_set.end(), std::back_inserter(uniques));
        int bgs = 0;
        
        for(int i = 0; i<uniques.size(); i++){
            for (int j = i+1; j<uniques.size(); j++) {
                int len = alternating(s, uniques[i], uniques[j]);
                if(len>0 && len>bgs){
                    bgs = len;
                }
            } 
        }
        
        return bgs;
    }
    
  • + 0 comments
    def alternate(s):
        # Write your code here
        max_length = 0
        uniques = set(s)
        for a, b in combinations(uniques, 2):
            filtered = [i for i in s if i == a or i == b]
            l = len(filtered)
            valid = True
            for i in range(1, l):
                if filtered[i] == filtered[i-1]:
                    valid = False
                    break
                    
            if l > max_length and valid:
                max_length = l
                
        return max_length
    
  • + 0 comments

    Fastest Solution in C++

    include

    include

    using namespace std;

    int main(){ ios::sync_with_stdio(false); cin.tie(nullptr);

    int n;
    cin >> n;
    cin.ignore();
    
    string s;
    cin >> s;
    
    vector<int> freq(26); 
    
    for(char c : s) ++freq[c - 'a'];
    
    int max_len = 0;
    for(char c1 = 'a'; c1 <= 'z'; ++c1) {
        if(freq[c1 - 'a'] == 0) continue;
    
        for(char c2 = c1 + 1; c2 <= 'z'; ++c2) {
            if(freq[c2 - 'a'] == 0) continue;
            if(abs(freq[c1 - 'a'] - freq[c2 - 'a']) > 1) continue;
            if(freq[c1 - 'a'] + freq[c2 - 'a'] < max_len) continue;
    
            char last = '\0'; int len = 0;
    
            for(char c : s) {
                if(c == last) { len = 0; break; }
                if(c != c1 && c != c2) continue;
                last = c; ++len;
            }
    
            max_len = max(max_len, len);
        }
    }
    
    cout << max_len << endl;
    
    return 0;
    

    }

  • + 0 comments

    return 0 and be surprised