Alternating Characters - HackerRank Solution.
NOTE: If you are copying my code then its an advice to you to copy it after downloading it to avoid any kind of compilation error its link is available at the bottom of the source code.
You are given a string containing characters and 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.
For example, given the string , remove an at positions and to make in deletions.
Function Description
Complete the alternatingCharacters function in the editor below. It must return an integer representing the minimum number of deletions to make the alternating string.
alternatingCharacters has the following parameter(s):
- s: a string
Input Format
The first line contains an integer , the number of queries.
The next lines each contain a string .
The next lines each contain a string .
Constraints
- Each string will consist only of characters and
Output Format
For each query, print the minimum number of deletions required on a new line.
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 doesn't have matching consecutive characters.
SOURCE CODE
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
// Complete the alternatingCharacters function below.
static int alternatingCharacters(String s) {
int count=0;
for(int i=0;i<s.length()-1;i++)
{
if(s.charAt(i)==s.charAt(i+1))
count++;
}
return(count);
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int q = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int qItr = 0; qItr < q; qItr++) {
String s = scanner.nextLine();
int result = alternatingCharacters(s);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedWriter.close();
scanner.close();
}
}
Click here to Download
RESULT
If you have any question then leave a comment below I will do my best to answer that question.
Did you know there is a 12 word sentence you can tell your partner... that will trigger intense emotions of love and instinctual attractiveness for you deep within his chest?
ReplyDeleteBecause deep inside these 12 words is a "secret signal" that fuels a man's instinct to love, idolize and care for you with all his heart...
12 Words Who Fuel A Man's Desire Impulse
This instinct is so hardwired into a man's mind that it will make him work harder than before to build your relationship stronger.
In fact, fueling this dominant instinct is absolutely important to achieving the best ever relationship with your man that the moment you send your man one of the "Secret Signals"...
...You'll soon notice him open his soul and heart to you in a way he never experienced before and he'll see you as the only woman in the world who has ever truly tempted him.