Longest palindrome in a string
Complexity

Your answer is \$O(n^3)\$ and not \$O(n^2)\$. You have three levels of nested loops... two inside the longestPalindrome() method and another inside the isPalindrome().

Can this problem be solved with less complexity? I believe it is possible to do an \$O(n^2)\$ solution ... I'll have to think it through though.

Test-Cases

Your code may have passed the test cases, but I don't believe your code works every time... consider this:


                if(s.substring(i, j+1).length()&gt;longestPalindrome.length()){
                    longestPalindrome = s.substring(i, j+1);
                    return longestPalindrome;
                }



You return the first palindrome you find.... longestPalindrome starts off as the empty String (length 0), and you return the first value that is longer. Your code could simply be equally broken as:

   if(s.substring(i, j+1).length() &gt; 0){
       return s.substring(i, j+1);
   }


I don't know what the test cases are, but they need work.

Nit-picks


giving a variable the same name as a method is 'wrong'... : longestPalindrome
you calculate s.substring(i, j+1) three times in the following block:


       if(isPalindrome(s.substring(i,j+1))){
           if(s.substring(i, j+1).length() &gt; longestPalindrome.length()){
               longestPalindrome = s.substring(i, j+1);
               return longestPalindrome;
           }
       }




Performance

Your loops could be trimmed down (a lot). These optimizations do not affect the complexity, but they do reduce the computational overhead:


for(int j = s.length()-1; j &gt;= 0 &amp;&amp; j != i; j--){



has redundant j-conditions. The j &gt;= 0 can be dropped since the j != 1 will be encountered first.

Also, j is only ever used in a +1 context. You may as well change the loop conditions to be:

       for(int j = s.length(); j &gt; i; j--){


and then replace all the j + 1 references inside the loop with plain j.

Conclusion

I am not satisfied that this solution reliably finds the longest palindrome....