Count characters with same neighbors

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, k

Input: str = “eeeeeeee”
Output: 8
Characters with same adjacent characters are e, e, e, e, e, e, e, e

Approach:

  1. If the length of string is less than 3 then return the length of the string.
  2. Initialize the count with 2 as first and last will always be counted.
  3. Start traversing the string.
    • Check if the previous and next character of current character are same.
    • Increment count, if yes.
  4. Return count.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Discovering ways to develop a plane for soaring career goals

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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.