We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Alternating Characters
- Discussions
Alternating Characters
Alternating Characters
Sort by
recency
|
1971 Discussions
|
Please Login in order to post a comment
Here are my c++ approaches of solving this, video explanation here : https://youtu.be/KoDlxS38_ig
Solution 1 : Using loop
Solution 2 : Using regex
Here is a Python3 implemention. It is intentionally verbose to ensure explainability:
Java Solution for this with single forloop with O(n) Complexity
public static int alternatingCharacters(String s) { // Write your code here StringBuffer buffer = new StringBuffer(); int count = 0; for(int i = 1; i < s.length(); i++){ if(s.charAt(i) == s.charAt(i - 1)){ count++;
}
} return count; }``
def alternatingCharacters(s):