Hello Programmers, In this post, you will learn how to solve HackerRank Alternating Characters Solution. This problem is a part of the HackerRank Algorithms Series.
One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.
HackerRank Alternating Characters Solution
Task
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. To do this, you are allowed to delete zero or more characters in the string.
Your task is to find the minimum number of required deletions.
Example
s = AABAAB
Remove an A at positions 0 and 3 to make s = ABAB in 2 deletions.
Function Description
Complete the alternatingCharacters function in the editor below.
alternatingCharacters has the following parameter(s):
- string s: a string
Returns
- int: the minimum number of deletions required
Input Format
The first line contains an integer q, the number of queries.
The next q lines each contain a string s to analyze.
Constraints
- 1 <= q <= 10
- 1 <= length of s <= 105
- Each string s will consist only of characters A and B.
Sample Input
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
Sample Output
3
4
0
0
4
Explanation
The characters marked red are the ones that can be deleted so that the string does not have matching adjacent characters.
HackerRank Alternating Characters Solutions
Alternating Characters Solution in C
#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("%u\n",count); count=0; } return 0; }
Alternating Characters Solution in Cpp
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin>>t; while(t--) { string s;int c=0,a=0; cin>>s; for(int i=1;s[i]!='\0';i++) { if((s[i]==65 && s[a]==66)||(s[i]==66 &&s[a]==65)) { a=i; // cout<<a<<endl; } else{ c++; //cout<<c<<" "<<i<<endl; } } cout<<c<<endl; } return 0; }
Alternating Characters Solution in Java
import java.util.Scanner; public class Solution { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner s = new Scanner(System.in); int t = s.nextInt(); s.nextLine(); while(t-- > 0) { int count = 0; String str = s.nextLine(); for(int i=1;i<str.length();i++) { if(str.charAt(i)==str.charAt(i-1)) { count++; } } System.out.println(count); } } }
Alternating Characters Solution in Python
t = int(raw_input()) for j in range(t): res=0 str = raw_input() if 'A' not in str or 'B' not in str: res = len(str)-1 print res continue i=0 while i<(len(str)-1): if str[i+1]==str[i]: res+=1 i+=1 print res
Alternating Characters Solution using JavaScript
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); });
Alternating Characters Solution in Scala
object Solution extends App { for (T <- 1 to readInt) { val s = readLine var i = 0 var ans = 0 while (i < s.length) { var j = i var cnt = 0 while (j < s.length && s(i) == s(j)) { j += 1 cnt += 1 } ans += cnt - 1 i = j } println(ans) } }
Alternating Characters Solution in Pascal
(* Enter your code here. Read input from STDIN. Print output to STDOUT *) program abab; var T : integer; word : ansistring; result : array [1..10] of longint; i : integer; j : longint; begin readln(T); for i := 1 to T do begin result[i] := 0; readln(word); for j := 1 to (length(word)-1) do if word[j] = word[j+1] then result[i] := result[i] + 1; end; for i := 1 to T do writeln(result[i]); end.
Disclaimer: This problem (Alternating Characters) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.