Java clear solution 46ms
find the next Palindrome and check whether it\'s Prime or not\n\nI find the next Palindrome code in https://stackoverflow.com/questions/7934519/a-better-algorit
find the next Palindrome and check whether it\'s Prime or not\n\nI find the next Palindrome code in https://stackoverflow.com/questions/7934519/a-better-algorithm-to-find-the-next-palindrome-of-a-number-string\n\n```\nimport java.math.BigInteger;\n\nclass Solution {\n    public int primePalindrome(int N) {\n        if(N==1){\n            return 2;\n        }\n        \n        while(!isPalin(N)){\n            N++;\n        }\n        \n        if(isPrime(N)){\n            return N;\n        }\n        \n        while(true){\n            N = Integer.parseInt(nextPalindrome(Integer.toString(N)));\n            if(isPrime(N)){\n                return N;\n            }\n        }\n\n    }\n    \n    public String nextPalindrome(String num) {\n        int len = num.length();\n        String left = num.substring(0, len / 2);\n        String middle = num.substring(len / 2, len - len / 2);\n        String right = num.substring(len - len / 2);\n\n        if (right.compareTo(reverse(left)) < 0)\n            return left + middle + reverse(left);\n\n        String next = new BigInteger(left + middle).add(BigInteger.ONE).toString();\n        return next.substring(0, left.length() + middle.length())\n             + reverse(next).substring(middle.length());\n    }\n\n    private String reverse(String s) {\n        return new StringBuilder(s).reverse().toString();\n    }\n    \n    private boolean isPalin(int num){\n        if(num<10) return true;\n        String str = Integer.toString(num);\n        int i = 0;\n        int j = str.length()-1;\n        while(i<j){\n            if(str.charAt(i)!=str.charAt(j)){\n                return false;\n            }\n            i++;\n            j--;\n        }\n        return true;\n    }\n    \n    private boolean isPrime(int num){\n        if(num==1) return false;\n        if(num==2 || num==3 || num==5 || num==7) return true;\n        for(int i = 2;i<(int)Math.sqrt(num)+1;i++){\n            if(num%i==0){\n                return false;\n            }\n        }\n        return true;\n    }\n    \n}\n```
[]