fork download
  1. class Solution {
  2. fun palindrome(s: String, l: Int, r: Int) : Boolean {
  3. var u = l
  4. var v = r-1
  5. while (u < v)
  6. if (s[u++] != s[v--])
  7. return false
  8. return true }
  9. fun longestPalindrome(s: String): String {
  10. val n = s.length
  11. for (k in n downTo 2)
  12. for (l in 0 until n-k+1)
  13. if (palindrome(s,l,l+k))
  14. return s.substring(l,l+k)
  15. return s.substring(0,1) } }
  16.  
  17. fun main(args: Array<String>) {
  18. print(Solution().longestPalindrome(readLine()!!)) }
Success #stdin #stdout 0.08s 36384KB
stdin
babad
stdout
bab