-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125ValidPalindrome.java
More file actions
35 lines (29 loc) · 978 Bytes
/
125ValidPalindrome.java
File metadata and controls
35 lines (29 loc) · 978 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
// Done as part of Facebook (Meta) coding interview prep
// 12/16/2021
// A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward.
// Alphanumeric characters include letters and numbers.
// Given a string s, return true if it is a palindrome, or false otherwise.
class Solution {
public boolean isPalindrome(String s)
{
if(s == "" || s == " ")
{
return true;
}
//removing non-alphanumeric characters
s = s.replaceAll("[^a-zA-Z0-9]", "");
s = s.toLowerCase();
//System.out.println(s);
int i, j = 0;
j = s.length()-1;
for(i = 0; i < s.length()/2; i++)
{
if(!(s.charAt(i)==(s.charAt(j))))
{
return false;
}
j--;
}
return true;
}
}