Problem Statement
Shashank likes strings in which consecutive characters are different. For example, he likes ABABA , while he doesn't like ABAA . Given a string containing characters A and B only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string.
Your task is to find the minimum number of required deletions.
Input Format
The first line contains an integerT i.e. the number of test cases.
NextT lines contain a string each.
The first line contains an integer
Next
Output Format
Print minimum number of required steps for each test case.
Print minimum number of required steps for each test case.
Constraints
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; /** * @author Rakesh KR */ public class Solution { public static void main(String[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int limit = Integer.parseInt(in.readLine()); if(limit>=1 && limit<=10){ for(int i = 0 ; i< limit ;i++){ StringBuffer sbStr = new StringBuffer(in.readLine()); if(sbStr.length()>=1 && sbStr.length()<=10*10*10*10*10){ StringBuffer delSbStr = new StringBuffer(); for(int j=0 ; j<sbStr.length()-1 ; j++){ if(sbStr.charAt(j)==sbStr.charAt(j+1)){ delSbStr.append(sbStr.charAt(j)); } } System.out.println(delSbStr.length()); sbStr = null; delSbStr = null; } } } } } |
Comments
Post a Comment