Created
June 27, 2021 23:05
-
-
Save JonathanLalou/65f50b2d54f64cc728190bc0baa32326 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
import java.security.*; | |
import java.text.*; | |
import java.util.*; | |
import java.util.concurrent.*; | |
import java.util.function.*; | |
import java.util.regex.*; | |
import java.util.stream.*; | |
import static java.util.stream.Collectors.joining; | |
import static java.util.stream.Collectors.toList; | |
class Result { | |
/* | |
* Complete the 'alternatingCharacters' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts STRING s as parameter. | |
*/ | |
public static int alternatingCharacters(String s) { | |
List<Character> cars = s.chars().mapToObj(c -> (char) c).collect(Collectors.toList()); | |
int counter = 0; | |
Character current=null; | |
// Write your code here | |
for (int i=0; i < cars.size(); i++){ | |
if(cars.get(i)!=current){ | |
current = cars.get(i); | |
} else { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
} | |
public class Solution { | |
public static void main(String[] args) throws IOException { | |
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); | |
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); | |
int q = Integer.parseInt(bufferedReader.readLine().trim()); | |
IntStream.range(0, q).forEach(qItr -> { | |
try { | |
String s = bufferedReader.readLine(); | |
int result = Result.alternatingCharacters(s); | |
bufferedWriter.write(String.valueOf(result)); | |
bufferedWriter.newLine(); | |
} catch (IOException ex) { | |
throw new RuntimeException(ex); | |
} | |
}); | |
bufferedReader.close(); | |
bufferedWriter.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
10', overcomplex,
List
was not needed