Given a string, the task is to find the number of characters with the same adjacent characters.
Note: First and the last character will always be counted as they will have only one adjacent character.
Examples:
Input: str = “egeeksk”
Output: 4
Characters with same adjacent characters are e, g, s, kInput: str = “eeeeeeee”
Output: 8
Characters with same adjacent characters are e, e, e, e, e, e, e, e
Approach:
- If the length of string is less than 3 then return the length of the string.
- Initialize the count with 2 as first and last will always be counted.
- Start traversing the string.
- Check if the previous and next character of current character are same.
- Increment count, if yes.
- Return count.
Below is the implementation of above approach:
C++
// C++ implementation of bove approach #include <bits/stdc++.h> using namespace std; // Function to count the characters // with same adjacent chracters int countChar(string str) { int n = str.length(); // if length is less than 3 // then return length as there // will be only two characters if (n <= 2) return n; int count = 2; // Traverse the string for (int i = 1; i < n - 1; i++) // Increment the count if the previous // and next character is same if (str[i - 1] == str[i + 1]) count++; // Return count return count; } // Driver code int main() { string str = "egeeksk"; cout << countChar(str); return 0; } |
Java
// Java implementation of the above approach class GFG { // Function to count the characters // with same adjacent chracters static int countChar(String str) { int n = str.length(); // if length is less than 3 // then return length as there // will be only two characters if (n <= 2) return n; int count = 2; // Traverse the string for (int i = 1; i < n - 1; i++) // Increment the count if the previous // and next character is same if (str.charAt(i - 1) == str.charAt(i + 1)) count++; // Return count return count; } // Driver code public static void main(String []args) { String str = "egeeksk"; System.out.println(countChar(str)); } } // This code is contributed // by ihritik |
Python3
# Python 3 implementation of bove approach # Function to count the characters # with same adjacent chracters def countChar(str): n = len(str) # if length is less than 3 # then return length as there # will be only two characters if (n <= 2): return n count = 2 # Traverse the string for i in range(1, n - 1): # Increment the count if the previous # and next character is same if (str[i - 1] == str[i + 1]): count += 1 # Return count return count # Driver code if __name__ == '__main__': str = "egeeksk" print(countChar(str)) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of above approach using System; class GFG { // Function to count the characters // with same adjacent chracters static int countChar(String str) { int n = str.Length; // if length is less than 3 // then return length as there // will be only two characters if (n <= 2) return n; int count = 2; // Traverse the string for (int i = 1; i < n - 1; i++) // Increment the count if the previous // and next character is same if (str[i - 1] == str[i + 1]) count++; // Return count return count; } // Driver code public static void Main() { String str = "egeeksk"; Console.WriteLine(countChar(str)); } } // This code is contributed // by Subhadeep |
PHP
<?php // PHP implementation of bove approach // Function to count the characters // with same adjacent chracters function countChar($str) { $n = strlen($str); // if length is less than 3 // then return length as there // will be only two characters if ($n <= 2) return $n; $count = 2; // Traverse the string for ($i = 1; $i < $n - 1; $i++) // Increment the count if the previous // and next character is same if ($str[$i - 1] == $str[$i + 1]) $count++; // Return count return $count; } // Driver code $str = "egeeksk"; echo countChar($str); // This code is contributed by Sach ?> |
4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Sum of Nodes and respective Neighbors on the path from root to a vertex V
- Check whether two strings contain same characters in same order
- Min flips of continuous characters to make all characters same in a string
- String with k distinct characters and no same characters adjacent
- Minimum characters that are to be inserted such that no three consecutive characters are same
- Minimum characters to be replaced to make frequency of all characters same
- Count characters at same position as in English alphabet
- Count substrings with same first and last characters
- Recursive solution to count substrings with same first and last characters
- Maximum count of sub-strings of length K consisting of same characters
- Count of sub-strings that do not contain all the characters from the set {'a', 'b', 'c'} at the same time
- Count number of substrings of a string consisting of same characters
- Count of substrings of a given Binary string with all characters same
- Count of Substrings with at least K pairwise Distinct Characters having same Frequency
- Count of ungrouped characters after dividing a string into K groups of distinct characters
- Rearrange a string so that all same characters become d distance away
- Rearrange a string so that all same characters become atleast d distance away
- Check if a string has all characters with same frequency with one variation allowed
- Rearrange characters in a string such that no two adjacent are same
- Group words with same set of characters
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

