-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathResult.java
More file actions
27 lines (22 loc) · 708 Bytes
/
Result.java
File metadata and controls
27 lines (22 loc) · 708 Bytes
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
package hackrank.algorithm.string.alternate;
/**
* @see <a href="https://www.hackerrank.com/challenges/alternating-characters">Alternating Characters</a>
*/
public class Result {
/**
* @param s value containing only A and B characters
* @return Minimum number of deletions required
*/
public static int alternatingCharacters(String s) {
char[] characters = s.toCharArray();
int deletions = 0;
for (int i = 1; i < characters.length; i++) {
char previous = characters[i - 1];
char current = characters[i];
if (previous == current) {
deletions++;
}
}
return deletions;
}
}