Advertisement
notFalkon

longestStreak method

Nov 13th, 2022
2,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. public class MyProgram
  2. {
  3. public static void main(String[] args)
  4. {
  5. System.out.println("e5");
  6. longestStreak("teeeeest");
  7. }
  8.  
  9. public static void longestStreak (String str) {
  10. char char1;
  11. char char2 = ' ';
  12. int count = 1;
  13. char letter = ' ';
  14.  
  15. int max = Integer.MIN_VALUE;
  16.  
  17. for (int i = 0; i < str.length(); i++) {
  18. char1 = str.charAt(i);
  19. if(i < str.length()-1) {
  20. char2 = str.charAt(i+1);
  21. }
  22.  
  23. if (char1 == char2) {
  24. count++;
  25. }
  26. else {
  27. if(max >= Math.max(max, count)) {
  28. letter = char1;
  29. }
  30. max = Math.max(max, count);
  31. count = 1;
  32. }
  33. }
  34. System.out.println(letter + " " + max);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement