Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Max palindrome string
/** * Max palindrome string * O(N ^ 2) algorithm */ import java.util.*; class Rextester { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String maxPalindrome = ""; int maxSizePalindrome = 0; for(int i = 0; i < str.length(); i++) { for(int j = i + 1; j < str.length(); j++) { String temp = str.substring(i, j + 1); if(isPalindrome(temp) && temp.length() > maxSizePalindrome) { maxSizePalindrome = temp.length(); maxPalindrome = temp; } } } System.out.println("Palindrome with max length = " + maxPalindrome + " (length = " + maxSizePalindrome + ")"); } public static boolean isPalindrome(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString().equals(str); } }
run
|
edit
|
history
|
help
0
Java Constructors
JAVA # MEVSİMLER
test1
Tech No.
Size and signedness of Java ints and longs
uraian angka
1.6
상속2
Hello world
Java Switch