Alternating Characters

Sort by

recency

|

1984 Discussions

|

  • + 0 comments
    def alternatingCharacters(s):
        # Write your code here
        res = 0
        for i in range(1, len(s)):
            if s[i-1] == s[i]:
                res += 1
        return res
    
    what do you think of this code for 'You are given a string containing characters  and  only. Your task is to change it into a string such that there are no matching adjacent characters. To do this, you are allowed to delete zero or more characters in the string.
    Your task is to find the minimum number of required deletions.' ??
    
  • + 0 comments
    int alternatingCharacters(string s) {
        if (s.find_first_not_of(s[0]) == string::npos) return s.size()-1;
        
        int del = 0;
        int len = s.size();
        
        for(int i = 0; i<len; i++){
            
            if((i+1)<len && s[i]==s[i+1]){
                del++;
            }
            
        }  
        
        return del;
    }
    
  • + 0 comments
    # Alternating Characters 🅰 🅱 🅰 🅱
    def alternating_characters(s):        
        return sum([ 1 for i in range(len(s)-1) if s[i] == s[i+1] ])
    
  • + 0 comments
    def alternatingCharacters(s):
        # Write your code here
        i=0
        count = 0
        s = list(s)
        while i < len(s) - 1:
            if s[i] == s[i+1]: 
                count+=1
                s.remove(s[i+1])
                continue
            i+=1
        return count
    
  • + 0 comments

    Java:

    public static int alternatingCharacters(String s) {
        int deletions = 0;
    
        for (int i = 0; i < s.length() - 1; i++) {
            if (s.charAt(i) == s.charAt(i + 1)) {
                deletions++;
            }
        }
    
        return deletions;
    }