Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerRank Alternating Characters problem solution

YASH PAL, 31 July 202410 September 2024

In this HackerRank Alternating Characters Interview preparation kit problem You are given a string containing characters A and B only. Your task is to change it into a string such that there are no matching adjacent characters.

HackerRank Alternating Characters Interview preparation kit solution

Problem solution in Python programming.

def f(s):
    return sum(1 for c1, c2 in zip(s, s[1:]) if c1 == c2)

t = int(input())
for _ in range(t):
    print(f(input()))

Problem solution in Java Programming.

import java.io.*;
import java.util.*;
import java.math.*;


public class Solution {
	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;
	
	
	static void solve() throws Exception {
		int t = nextInt();
		for (int tt = 0; tt < t; ++tt) {
			String s = next();
			int res = 0;
			for (int i = 0; i < s.length(); ++i) {
				if (i - 1 >= 0 && s.charAt(i - 1) == s.charAt(i)) {
					++res;
				}
			}
			out.println(res);
		}
	}

	static int sqr(int x) {
		return x*x;
	}
	
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static long nextLong() throws IOException {
		return Long.parseLong(next());
	}

	static double nextDouble() throws IOException {
		return Double.parseDouble(next());
	}

	static BigInteger nextBigInteger() throws IOException {
		return new BigInteger(next());
	}
	
	static String next() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}
	
	static String nextLine() throws IOException {
		tok = new StringTokenizer("");
		return in.readLine();
	}

	static boolean hasNext() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			String s = in.readLine();
			if (s == null) {
				return false;
			}
			tok = new StringTokenizer(s);
		}
		return true;
	}

	public static void main(String args[]) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			//in = new BufferedReader(new FileReader("input.in"));
			//out = new PrintWriter(new FileWriter("output.out"));
			solve();
			in.close();
			out.close();
		} catch (Throwable e) {
			e.printStackTrace();
			java.lang.System.exit(1);
		}
	}
}

Problem solution in C++ programming.

#include <iostream>
#include <string>

using namespace std ;

int main()
{
    int test_cases;
    cin >> test_cases ;
    string str;
    int count  ;
    int j ;
    char prev ;
    for ( int i =0 ; i < test_cases ;i++)
    {
        cin >> str ;
        j = 1; prev = str[0]; count = 0 ; 
        while((int)str[j]!=0)
        {
            if(str[j] == prev)
                count ++ ;
            else
                prev = str[j] ;
            j++ ;
        }
        cout << count << endl ;
    }
    return 0 ;
}

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() 
{
    int t;
    long i=0;
    unsigned int count=0;
    char * c;
    scanf("%d",&t);
    c=(char *)malloc(sizeof(char)*(100002));
    while(t--)
    {
        scanf("%s",c);
        for(i=0; *(c+i); i++)
        {
            if(c[i]==c[i+1])
            {
                count++;
            }
        }
        printf("%un",count);
        count=0;
    }
      
    return 0;
}

Problem solution in JavaScript programming.

function processData(input) {
  var inputArray = input.split('n');
  var t = parseInt(inputArray[0]);
  var pointer = 1;
  while (pointer <= t) {
    var target = inputArray[pointer];
    var count = 0;
    for (var i=1; i<target.length; i++) {
      if (target[i] == target[i-1]) {
        count += 1;
      }
    }
    console.log(count);
    pointer += 1;
  }
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

coding problems solutions interview prepration kit

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes