File tree Expand file tree Collapse file tree 1 file changed +27
-7
lines changed Expand file tree Collapse file tree 1 file changed +27
-7
lines changed Original file line number Diff line number Diff line change 1
1
public boolean isPalindrome (String s ) {
2
- StringBuilder content = new StringBuilder ();
3
- for (int i = 0 ; i < s .length (); i ++)
4
- if (Character .isLetterOrDigit (s .charAt (i )))
5
- content .append (s .charAt (i ));
6
- content = new StringBuilder (content .toString ().toLowerCase ());
7
- String value = content .toString ();
8
- return value .equals (content .reverse ().toString ());
2
+
3
+ int i = 0 ;
4
+ int j = s .length () - 1 ;
5
+ while (i < j ) {
6
+
7
+ Character start = s .charAt (i );
8
+ Character end = s .charAt (j );
9
+
10
+ if (!Character .isLetterOrDigit (start )) {
11
+ i ++;
12
+ continue ;
13
+ }
14
+
15
+ if (!Character .isLetterOrDigit (end )) {
16
+ j --;
17
+ continue ;
18
+ }
19
+
20
+ if (Character .toLowerCase (start ) != Character .toLowerCase (end )) {
21
+ return false ;
22
+ }
23
+
24
+ i ++;
25
+ j --;
9
26
}
27
+
28
+ return true ;
29
+ }
You can’t perform that action at this time.
0 commit comments