fork 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 s="babad";
  13. int n = s.length();
  14. String ans ="";
  15. for(int i=0;i<n;i++){
  16. for(int j=i;j<n;j++){
  17. String ts = s.substring(i,j+1);
  18. if(isPalindrome(ts) && ts.length()>ans.length()){
  19. ans =ts;
  20. }
  21. }
  22. }
  23. System.out.println(ans);
  24.  
  25. }
  26.  
  27. static public boolean isPalindrome(String s){
  28. int l=0,r=s.length()-1;
  29. while(l<=r){
  30. if(s.charAt(l) != s.charAt(r)){
  31. return false;
  32. }
  33. l++;
  34. r--;
  35. }
  36. return true;
  37. }
  38. }
  39.  
  40.  
Success #stdin #stdout 0.07s 54604KB
stdin
Standard input is empty
stdout
bab