Count characters with same neighbors - GeeksforGeeks

28 Jun, 2021
Given a string, the task is to find the number of characters with the same adjacent characters. Note: First and 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 the 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 characters of the current character are the same.Increment count, if yes.Return count.
If the length of the 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 characters of the current character are the same.Increment count, if yes.
Check if the previous and next characters of the current character are the same.
Increment count, if yes.
Return count.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // Function to count the characters// with same adjacent charactersint 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 codeint main(){    string str = "egeeksk";    cout << countChar(str);     return 0;}
// Java implementation of the above approach             class GFG{                  // Function to count the characters         // with same adjacent characters             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
# Python 3 implementation of above approach # Function to count the characters# with same adjacent charactersdef 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 codeif __name__ == '__main__':    str = "egeeksk"    print(countChar(str))     # This code is contributed by# Surendra_Gangwar
// C# implementation of above approachusing System; class GFG{ // Function to count the characters// with same adjacent charactersstatic 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 codepublic static void Main(){    String str = "egeeksk";    Console.WriteLine(countChar(str));}} // This code is contributed// by Subhadeep
<?php// PHP implementation of above approach // Function to count the characters// with same adjacent charactersfunction 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?>
<script> // Javascript implementation of above approach // Function to count the characters// with same adjacent charactersfunction countChar(str){    var n = str.length;     // if length is less than 3    // then return length as there    // will be only two characters    if (n <= 2)        return n;    var count = 2;     // Traverse the string    for (var 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 codevar str = "egeeksk";document.write( countChar(str)); </script>
4
 
ihritik
tufan_gupta2000
Sach_Code
SURENDRA_GANGWAR
ManasChhabra2
rrrtnx
as5853535
ruhelaa48
Searching
Strings
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Best First Search (Informed Search)
Program to remove vowels from a String
3 Different ways to print Fibonacci series in Java
Recursive Programs to find Minimum and Maximum elements of array
Interpolation search vs Binary search
Reverse a string in Java
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
C++ Data Types
Write a program to print all permutations of a given string