fork(5) 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. System.out.println("Longest Substring is " + getLongestSubstring("abbcccdf"));
  12. }
  13.  
  14. public static String getLongestSubstring(String s) {
  15. int length = 1;
  16.  
  17. String longestString = "";
  18.  
  19. for (int i = 0; i < s.length(); i++) {
  20. StringBuilder str = new StringBuilder();
  21. str.append(s.charAt(i));
  22.  
  23. for (int j = i + 1; j < s.length(); j++) {
  24. if (s.charAt(i) == s.charAt(j)) {
  25. str.append(s.charAt(j));
  26. } else {
  27. break;
  28. }
  29. }
  30. if (length < str.length()) {
  31. length = str.length();
  32. longestString = str.toString();
  33. }
  34.  
  35. }
  36. return longestString;
  37. }
  38. }
Success #stdin #stdout 0.07s 380224KB
stdin
Standard input is empty
stdout
Longest Substring is ccc