forked from NIKHILSINGH1510/Test_Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.java
More file actions
42 lines (37 loc) · 983 Bytes
/
palindrome.java
File metadata and controls
42 lines (37 loc) · 983 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
37
38
39
40
41
42
class Solution {
public boolean isPalindrome(String s) {
s=s.toLowerCase();
s=s.trim();
s=s.replaceAll("\\s","");
s=s.replaceAll("[^a-zA-Z0-9]", "");
Stack<Character> st=new Stack<>();
if(s.length()%2==0)
{
for(int i=0;i<s.length()/2;i++)
{
st.push(s.charAt(i));
}
for(int i=s.length()/2;i<s.length();i++)
{
if(st.pop()!=s.charAt(i))
{
return false;
}
}
}
else{
for(int i=0;i<s.length()/2;i++)
{
st.push(s.charAt(i));
}
for(int i=s.length()/2+1;i<s.length();i++)
{
if(st.pop()!=s.charAt(i))
{
return false;
}
}
}
return true;
}
}