-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptimalPalindrome.java
More file actions
36 lines (35 loc) · 889 Bytes
/
Copy pathOptimalPalindrome.java
File metadata and controls
36 lines (35 loc) · 889 Bytes
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
import java.util.*;
public class Main
{
public static void main(String[] args) {
String s1=" abcba";
String s2="abca";
String s3="@ab$ba ";
System.out.println(Palindrome(s1));
System.out.println(Palindrome(s2));
System.out.println(Palindrome(s3));
}
private static boolean Palindrome(String str){
if(str.length()==0 || str.length()==1){
return false;
}
int left=0;
int right=str.length()-1;
while(left<right){
if(!Character.isAlphabetic(str.charAt(left))){
left++;
}
else if(!Character.isAlphabetic(str.charAt(right))){
right--;
}
else if(Character.toLowerCase(str.charAt(left)) != Character.toLowerCase(str.charAt(right))){
return false;
}
else{
left++;
right--;
}
}
return true;
}
}