File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
HackerRank/Java/Problem_Solving Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments