fork download
  1. public class Main
  2. {
  3. public static String getLongestSubstringNoRepeats( String string ){
  4. int iLongestSoFar = 0;
  5. int posLongestSoFar = 0;
  6. char charPrevious = 0;
  7. int xCharacter = 0;
  8. int iCurrentLength = 0;
  9. while( xCharacter < string.length() ){
  10. char charCurrent = string.charAt( xCharacter );
  11. iCurrentLength++;
  12. if( charCurrent == charPrevious ){
  13. if( iCurrentLength > iLongestSoFar ){
  14. iLongestSoFar = iCurrentLength;
  15. posLongestSoFar = xCharacter;
  16. }
  17. iCurrentLength = 1;
  18. }
  19. charPrevious = charCurrent;
  20. xCharacter++;
  21. }
  22. if( iCurrentLength > iLongestSoFar ){
  23. return string.substring( posLongestSoFar );
  24. } else {
  25. return string.substring( posLongestSoFar, posLongestSoFar + iLongestSoFar );
  26. }
  27. }
  28.  
  29. public static void main(String[] args)
  30. {
  31. System.out.println(getLongestSubstringNoRepeats("BABGAKKGIMN"));
  32. }
  33. }
Runtime error #stdin #stdout #stderr 0.07s 380160KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 13
	at java.lang.String.substring(String.java:1907)
	at Main.getLongestSubstringNoRepeats(Main.java:25)
	at Main.main(Main.java:31)