fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String setofletters = "aaakkcccccczz"; /* 15 */
  13. int output = runLongestIndex(setofletters);
  14. System.out.println("Longest run that first appeared in index : " + output);
  15. }
  16.  
  17. public static int runLongestIndex(String setofletters) {
  18. int maxCount = 0;
  19. int maxIndex = 0;
  20.  
  21. // loops each character in the string
  22. for (int i = 0; i < setofletters.length() - 1; ) {
  23. // new char sequence starts here
  24. char currChar = setofletters.charAt(i);
  25. int count = 1;
  26. int index = i;
  27. while ( (index < setofletters.length() - 1) &&
  28. (currChar == setofletters.charAt(++index)) ) {
  29. count++;
  30. }
  31.  
  32. if (count > maxCount) {
  33. maxIndex = i;
  34. maxCount = count;
  35. }
  36. i = index;
  37. }
  38. return maxIndex;
  39. }
  40. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
Longest run that first appeared in index : 5