File tree 1 file changed +12
-15
lines changed
1 file changed +12
-15
lines changed Original file line number Diff line number Diff line change 1
- /*
2
- Problem: https://leetcode.com/problems/valid-palindrome/
3
- Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward
4
- Concept : Two Pointers, String
5
- Time Complexity: O(n ), Runtime: 10ms
6
- Space Complexity: O(n ), Memory: 58.6MB
7
- */
1
+ /**
2
+ * <a href=" https://leetcode.com/problems/valid-palindrome/">week03-1.valid-palindrome</a>
3
+ * <li> Description: return true if it is a palindrome </li>
4
+ * <li>Topics : Two Pointers, String </li>
5
+ * <li> Time Complexity: O(N ), Runtime 13ms </li>
6
+ * <li> Space Complexity: O(N ), Memory 45.55MB </li>
7
+ */
8
8
class Solution {
9
9
public boolean isPalindrome (String s ) {
10
- String regex ="[^A-Za-z0-9]" ;
11
- String palindrome = s .replaceAll (regex ,"" ).toLowerCase (); //replaceAll(), toLowerCase(): O(n)
10
+ String str = s .toLowerCase ().replaceAll ("[^0-9a-z]" , "" );
12
11
13
- boolean answer = true ;
14
- for (int i =0 ; i <palindrome .length ()/2 ; i ++){
15
- if (palindrome .charAt (i ) != palindrome .charAt (palindrome .length ()-1 -i )) {
16
- answer = false ;
17
- break ;
12
+ for (int i = 0 ; i < str .length () / 2 ; i ++) {
13
+ if (str .charAt (i ) != str .charAt (str .length () - 1 - i )) {
14
+ return false ;
18
15
}
19
16
}
20
- return answer ;
17
+ return true ;
21
18
}
22
19
}
You can’t perform that action at this time.
0 commit comments