fork download
  1. /* package whatever; // don't place package name! */
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6.  
  7. public class Main {
  8. public static void main(String[] args) {
  9. String fileName = "24-168.txt";
  10. StringBuilder letterSequence = new StringBuilder();
  11. ArrayList<Integer> countList = new ArrayList<>();
  12.  
  13. try (BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName))) {
  14. String line = bufferedReader.readLine();
  15.  
  16. // String s = "AABBAABBCCDDDEFFGF"; // Uncomment this line to test with a fixed string
  17.  
  18. if (line != null && !line.isEmpty()) {
  19. letterSequence.append(line.charAt(0));
  20. countList.add(1);
  21.  
  22. for (int i = 1; i < line.length(); i++) {
  23. if (line.charAt(i) == line.charAt(i - 1)) {
  24. countList.set(countList.size() - 1, countList.get(countList.size() - 1) + 1);
  25. } else {
  26. letterSequence.append(line.charAt(i));
  27. countList.add(1);
  28. }
  29. }
  30. }
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34.  
  35. int maxLength = 0;
  36. for (int i = 2; i < letterSequence.length(); i++) {
  37. if (letterSequence.charAt(i - 2) < letterSequence.charAt(i - 1) && letterSequence.charAt(i - 1) < letterSequence.charAt(i)) {
  38. int length = countList.subList(i - 2, i + 1).stream().mapToInt(Integer::intValue).sum();
  39. maxLength = Math.max(length, maxLength);
  40. }
  41. }
  42.  
  43. System.out.println(maxLength);
  44. }
  45. }
Success #stdin #stdout #stderr 0.1s 54812KB
stdin
Standard input is empty
stdout
0
stderr
java.io.FileNotFoundException: 24-168.txt (No such file or directory)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:213)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:155)
	at java.base/java.io.FileInputStream.<init>(FileInputStream.java:110)
	at java.base/java.io.FileReader.<init>(FileReader.java:60)
	at Main.main(Main.java:13)