Alternating Characters

Sort by

recency

|

1968 Discussions

|

  • + 0 comments

    def alternatingCharacters(s):

        # Write your code here
    deletions=0
    for i in range(1,len(s)):
        if s[i-1]==s[i]:
            deletions+=1
    return deletions
    
  • + 0 comments

    Here are my c++ approaches of solving this, video explanation here : https://youtu.be/KoDlxS38_ig

    Solution 1 : Using loop

    int alternatingCharacters(string s) {
        int result = 0;
        for(int i = 1; i < s.size(); i++) if(s[i] == s[i-1]) result++;
        return result;
    }
    

    Solution 2 : Using regex

    int alternatingCharacters(string s) {
        regex re("A{2,}|B{2,}");
        return s.size() - regex_replace(s, re, "*").size();
    }
    
  • + 0 comments

    Here is my one line Python solution!

    def alternatingCharacters(s):
        return len([True for i in range(1, len(s)) if s[i] == s[i - 1]])
    
  • + 1 comment

    PYTHON CODE

    def alternatingCharacters(s):
        # Write your code here
        d=0
        sl=list(s)
        for i in range(0, len(sl)-1):
            if sl[i]==sl[i+1]:
                d+=1
        return d
    		
    
  • + 0 comments

    python MyAnswer

    def alternatingCharacters(s): i=0 j=1 count=0 while i