Sort by

recency

|

1141 Discussions

|

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

  • + 0 comments

    This is my Python solution ✅ Explanation: 1. I first generate all unique character pairs from the input string. 2. For each pair, I build a filtered string that only includes those two characters. 3. I then check if the filtered string alternates properly (i.e., no repeated consecutive characters). 4. If it passes, I update the maximum length found so far. 5. In the end, return the longest valid alternating string length or 0 if none found.

    📌 Note: Although this problem is tagged "Easy", it requires thoughtful implementation. I’d say it feels more like a Medium problem due to the character pairing and validation logic.

    Hope this helps others! 😊

    def alternate(s):
        # Write your code here
        def isConsecutive(st):
            if len(st) < 2:
                return True
            
            for i in range(1, len(st)):
                if st[i] == st[i-1]:
                    return True
            
            return False
        
        chars = list(set(s))
        candidates = []
        maxalter = float('-inf')
        
        for i in range(len(chars) - 1):
            for j in range(i + 1, len(chars)):
                candidates.append([chars[i], chars[j]])
        
        for cand in candidates:
            temp = ''
            for c in s:
                if c in cand:
                    temp += c
            if not isConsecutive(temp):
                maxalter = max(maxalter, len(temp))
        
        return maxalter if maxalter != float('-inf') else 0