Jewel Game (Hack The Interview V : Asia-Pacific)
Contest Link
import java.util.*;
class Solution{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String jewels = sc.next();
StringBuilder s = new StringBuilder();
s.append(jewels);
int count = 0;
while(true){
int temp = 0;
for(int i=0;i<s.length()-1;i++){
if(s.charAt(i)==s.charAt(i+1)){
count++;
s.deleteCharAt(i);
s.deleteCharAt(i);
temp++;
}
}
if(temp==0){
break;
}
}
System.out.println(count);
}
}
}
Comments
Post a Comment