-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path125.valid-palindrome.java
More file actions
29 lines (25 loc) · 1 KB
/
125.valid-palindrome.java
File metadata and controls
29 lines (25 loc) · 1 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
class Solution {
public boolean isPalindrome(String s) {
// remove spaces and non numes
// use 2 pointers to looop through the string
//i to start from the begining 0 and at the end newS.size-1
//while i<j
// compare char at pointer i if is equal to char at pointer
// increament i decrement j, if they meet without a false, then its a palindrome
// if at any point a we get a false, tehn we terminate and return false
//string newS = s.replaceAll("[^A-Za-z]+", "").toLowerCase();
//string newS =s.replaceAll("[^a-zA-Z ]", "").toLowerCase();
// String newS = s.replaceAll("[^A-Za-z]+", "").toLowerCase();
String newS = s.replaceAll("[^A-Za-z0-9]+", "").toLowerCase();
int i = 0;
int j = newS.length()-1;
while(i<j){
if(newS.charAt(i) != newS.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
}