GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/substring.java at master · Ayan0054/Java Expand file tree/ Copy pathsubstring.javaMore file actions 41 lines (35 loc) · 1.13 KB / Copy pathsubstring.javaFile metadata and controls41 lines (35 loc) · 1.13 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 class substring { // function to check if a substring // s[low..high] is a palindrome static boolean checkPal(String s, int low, int high) { while (low < high) { if (s.charAt(low) != s.charAt(high)) return false; low++; high--; } return true; } // function to find the longest palindrome substring static String getLongestPal(String s) { int n = s.length(); // all substrings of length 1 are palindromes int maxLen = 1, start = 0; // nested loop to mark start and end index for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { // check if the current substring is // a palindrome if (checkPal(s, i, j) && (j - i + 1) > maxLen) { start = i; maxLen = j - i + 1; } } } return s.substring(start, start + maxLen); } public static void main(String[] args) { String s = "forgeeksskeegfor"; System.out.println(getLongestPal(s)); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |