Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2202a60

Browse files
Update 0125-valid-palindrome.js
Added a new 2 pointer implementation that doesn't use RegEx (only primitives), and no copying of strings.
1 parentcdfcdd9 commit2202a60

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎javascript/0125-valid-palindrome.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,31 @@ var isPalindrome = function(s) {
5555
}
5656
returntrue;
5757
};
58+
59+
/**
60+
* 2 Pointer | Midde Convergence | No RegEx | No Copying
61+
* Time O(N) | Space O(1)
62+
* https://leetcode.com/problems/valid-palindrome/
63+
*@param {string} s
64+
*@return {boolean}
65+
*/
66+
varisPalindrome=function(s){
67+
constisAlpha=c=>(c.toLowerCase()>='a'&&c.toLowerCase()<='z')||c>='0'&&c<='9'
68+
69+
leti=0;
70+
letj=s.length-1;
71+
72+
while(i<j){
73+
if(!isAlpha(s.charAt(i))){
74+
i++
75+
}elseif(!isAlpha(s.charAt(j))){
76+
j--
77+
}elseif(s.charAt(i).toLowerCase()!==s.charAt(j).toLowerCase()){
78+
returnfalse
79+
}else{
80+
i++
81+
j--
82+
}
83+
}
84+
returntrue
85+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp