File tree Expand file tree Collapse file tree 1 file changed +4
-27
lines changed
Expand file tree Collapse file tree 1 file changed +4
-27
lines changed Original file line number Diff line number Diff line change 22// SC: O(n)
33class Solution {
44 public boolean isPalindrome (String s ) {
5- String target = checkString (s );
6- return checkPalindrome (target );
7- }
8-
9- private String checkString (String s ) {
10- StringBuilder sb = new StringBuilder ();
11- for (char c : s .toCharArray ()) {
12- if (c >= 'a' && c <= 'z' ) {
13- sb .append (c );
14- }
15- if (c >= 'A' && c <= 'Z' ) {
16- c = (char )(c - 'A' + 'a' );
17- sb .append (c );
18- }
19- if (c >= '0' && c <= '9' ) {
20- sb .append (c );
21- }
22- }
23- return sb .toString ();
24- }
5+ s = s .replaceAll ("[^a-zA-Z0-9]" , "" ).toLowerCase ();
256
26- private Boolean checkPalindrome (String target ) {
27- int start = 0 ;
28- int end = target .length () - 1 ;
7+ if (s .length () == 1 ) return true ;
298
30- while (start < end ) {
31- if (target .charAt (start ) != target .charAt (end )) return false ;
32- start += 1 ;
33- end -= 1 ;
9+ for (int i = 0 ; i < s .length () / 2 ; i ++) {
10+ if (s .charAt (i ) != s .charAt (s .length () - i - 1 )) return false ;
3411 }
3512 return true ;
3613 }
You can’t perform that action at this time.
0 commit comments