Sort by

recency

|

1140 Discussions

|

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