Skip to content

Commit 27387d4

Browse files
committed
Problem 071 Alternative Characters Counting Similar Character
1 parent a5d648d commit 27387d4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package hackerRank_JavaProblemSolving;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.util.Scanner;
7+
8+
public class Problem071_AlternatingCharacters_CoutningSimilarCharacter_inString {
9+
10+
// HINT: Count identical character in sequences
11+
static int alternatingCharacters(String s) {
12+
int count = 0;
13+
for (int i = 0 ; i < s.trim().length()-1; i++) {
14+
if (s.charAt(i) == s.charAt(i+1)) {
15+
count++;
16+
}
17+
}
18+
return count;
19+
}
20+
21+
private static final Scanner scanner = new Scanner(System.in);
22+
23+
@SuppressWarnings("unused")
24+
public static void main(String[] args) throws IOException {
25+
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
26+
27+
int q = scanner.nextInt(); // TO IGNORE DUE TO THE BUG OF INITIAL CODE
28+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
29+
30+
while(scanner.hasNextLine()) {
31+
bufferedWriter.write(String.valueOf(alternatingCharacters(scanner.next().toString())));
32+
bufferedWriter.newLine();
33+
}
34+
35+
bufferedWriter.close();
36+
37+
scanner.close();
38+
}
39+
}

0 commit comments

Comments
 (0)