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 , the number of queries.
The next lines each contain a string to analyze.
Constraints
- 1<=q<=10
- 1<=length of s <= 10^5
- 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.
Alternating Characters HackerRank 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 HackerRank Solution in C++
#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 HackerRank Solution in Java
import java.util.Scanner; public class Solution { 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 HackerRank 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 HackerRank Solution in C#
using System; namespace AlternatingCharacters { class Program { static void Main(string[] args) { int t = int.Parse(Console.ReadLine()); for (int i = 0; i < t; i++) { HandleTestCase(); } } static void HandleTestCase() { string str = Console.ReadLine(); int deletions = 0; char lastChar = 'C'; for (int i = 0; i < str.Length; i++) { if (str[i] == lastChar) { deletions++; } lastChar = str[i]; } Console.WriteLine(deletions); } } }
Alternating Characters HackerRank Challenge
Link – https://www.hackerrank.com/challenges/alternating-characters/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/the-full-counting-sort-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.
def alternatingCharacters(s):
return sum([1 for i in range(len(s)-1) if s[i] == s[i+1]])
The more simplest approach to solve in python.