File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
August Challenge/com/leetcode/AugustChallenge/week1 Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .leetcode .AugustChallenge .week1 ;
2+
3+ public class ValidPalindrome {
4+ public boolean isPalindrome (String s ) {
5+
6+ String str = preprocessInputString (s );
7+
8+ int left = 0 ;
9+ int right = str .length () - 1 ;
10+
11+ while (left < right ) {
12+ if (str .charAt (left ) != str .charAt (right ))
13+ return false ;
14+ left ++;
15+ right --;
16+ }
17+ return true ;
18+ }
19+
20+ public String preprocessInputString (String s ) {
21+ int n = s .length ();
22+ s = s .toLowerCase ();
23+ String result = "" ;
24+ for (int i = 0 ; i < n ; i ++) {
25+ char ch = s .charAt (i );
26+ if (Character .isLetterOrDigit (ch )) {
27+ result += ch ;
28+ }
29+ }
30+ return result ;
31+ }
32+
33+ public static void main (String [] args ) {
34+ String s = "A man, a plan, a canal: Panama" ;
35+ System .out .println (new ValidPalindrome ().isPalindrome (s ));
36+
37+ }
38+
39+ }
You can’t perform that action at this time.
0 commit comments