Count of Groups with Same Consecutive Digits

The program must accept an integer N as the input. The program must count the number of groups in the number where the same digit appears consecutively and print the count. A single digit without any consecutively same digit is not considered as a group.

Boundary Condition(s):
1 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains the count of groups with the same consecutive digits.

Example Input/Output 1:
Input:
1223334444

Output:
3

Explanation:
There are three groups with same consecutive digits in the number 1223334444 – “22”, “333”, and “4444”. Hence the output is 3.

Example Input/Output 2:
Input:
123456789

Output:
0

Explanation:
There are no groups with the same consecutive digits in the number 123456789. Hence the output is 0.

n = input().strip()
count = sum(1 for i in range(1, len(n)) if n[i] == n[i-1] and (i == 1 or n[i] != n[i-2]))
print(count)
#include<stdio.h>
#include<string.h>

int main() {
    char n[12];
    scanf("%s", n);
    int len = strlen(n), count = 0;
    for(int i = 1; i < len; i++) {
        if(n[i] == n[i-1] && (i == 1 || n[i] != n[i-2])) count++;
    }
    printf("%d", count);
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    int count = 0;
    for(int i = 1; i < n.size(); i++) {
        if(n[i] == n[i-1] && (i == 1 || n[i] != n[i-2])) count++;
    }
    cout << count;
    return 0;
}
import java.util.Scanner;

public class CountConsecutiveGroups {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int count = 0;
        for(int i = 1; i < n.length(); i++) {
            if(n.charAt(i) == n.charAt(i-1) && (i == 1 || n.charAt(i) != n.charAt(i-2))) count++;
        }
        System.out.println(count);
    }
}
Previous Article

Function isEvenParity – CTS PATTERN

Next Article

Create a Number by Doubling Each Digit

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *