Alternating Characters hacker rank solution,hackerrank.com | HackerRank Codes
BBBBB ⟹ B, 4 deletions
ABABABAB ⟹ ABABABAB, 0 deletions
BABABA ⟹ BABABA, 0 deletions
AAABBB ⟹ AB, 4 deletions
http://aruncyberspace.blogspot.com/2014/11/algorithms-warmup-alternating-characters.html
Read full article from Alternating Characters hacker rank solution,hackerrank.com | HackerRank Codes
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.
AAAA ⟹ A, 3 deletionsBBBBB ⟹ B, 4 deletions
ABABABAB ⟹ ABABABAB, 0 deletions
BABABA ⟹ BABABA, 0 deletions
AAABBB ⟹ AB, 4 deletions
http://aruncyberspace.blogspot.com/2014/11/algorithms-warmup-alternating-characters.html
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int count = 0; count < T; count++) {
char[] characters= sc.next().toCharArray();
int AlterCount=0;
for(int i=0;i<characters.length-1;i++)
{
if(characters[i]==characters[i+1])
{
AlterCount++;
}
}
System.out.println(AlterCount);
}
sc.close();
}
http://www.martinkysel.com/hackerrank-alternating-characters-solution/if
__name__
=
=
'__main__'
:
t
=
input
()
for
_
in
range
(t):
s
=
raw_input
()
delete_cnt
=
0
for
i
in
range
(
1
,
len
(s)):
if
s[i]
=
=
s[i
-
1
]:
delete_cnt
+
=
1
print
delete_cnt