Skip to content

Instantly share code, notes, and snippets.

@JonathanLalou
Created June 27, 2021 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanLalou/65f50b2d54f64cc728190bc0baa32326 to your computer and use it in GitHub Desktop.
Save JonathanLalou/65f50b2d54f64cc728190bc0baa32326 to your computer and use it in GitHub Desktop.
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();
}
}
@JonathanLalou
Copy link
Author

10', overcomplex, List was not needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment