Sort by

recency

|

1142 Discussions

|

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

  • + 0 comments
    public static int alternate(String str) {
        String[] uniqueCharacters = Arrays.stream(str.split("")).collect(Collectors.toSet()).toArray(new String[0]);
        int maxLength = 0;
        for(int i=0;i<uniqueCharacters.length-1; i++) {
            for(int j=i+1; j<uniqueCharacters.length; j++) {
                String firstChar = uniqueCharacters[i];
                String secondChar= uniqueCharacters[j];
                String newStr = str;
                List<String> lst = Arrays.stream(uniqueCharacters).filter(s -> !s.equals(firstChar) && !s.equals(secondChar)).collect(Collectors.toList());
                for(String l:lst) {
                    newStr = newStr.replaceAll(l, "");
                }
                if(isAlternateCharacters(newStr) & newStr.length() > maxLength) {
                    maxLength = newStr.length();
                }
            }
        }
        return maxLength;
    }   
    
    
    private static boolean isAlternateCharacters(String str) {
        if(str.length() == 1) return false;
    
        char firstchar = str.charAt(0);
        char secondchar = str.charAt(1);
        if(firstchar == secondchar) return false;
    
        for(int i=0; i<str.length(); i++) {
            if(i%2 == 0 && str.charAt(i) == firstchar) continue;
            if(i%2 != 0 && str.charAt(i) == secondchar) continue;
            return false;
        }
        return true;
    }
    
  • + 0 comments

    Here is problem solution in python, java, c++ c and javascript - https://programmingoneonone.com/hackerrank-two-characters-problem-solution.html