File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed
Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 18182 . [ Reverse Integer] ( ./Strings/reverse-integer.js )
19193 . [ First Unique Character in a String] ( ./Strings/first-unique-character-in-a-string.js )
20204 . [ Valid Anagram] ( ./Strings/valid-anagram.js )
21+ 5 . [ Valid Palindrome] ( ./Strings/valid-palindrome.js )
Original file line number Diff line number Diff line change 1+ // Задача на leetcode: https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/883/
2+
3+ //Способ 1:
4+
5+ var isPalindrome = function ( s ) {
6+ const string = s . replace ( / [ ^ 0 - 9 a - z ] / gi, "" ) . toLowerCase ( ) ;
7+ const reverseString = s
8+ . replace ( / [ ^ 0 - 9 a - z ] / gi, "" )
9+ . toLowerCase ( )
10+ . split ( "" )
11+ . reverse ( )
12+ . join ( "" ) ;
13+
14+ if ( string === reverseString ) {
15+ return true ;
16+ }
17+
18+ return false ;
19+ } ;
20+
21+ //Способ 2:
22+
23+ var isPalindrome2 = function ( s ) {
24+ const string = s . replace ( / [ ^ 0 - 9 a - z ] / gi, "" ) . toLowerCase ( ) ;
25+
26+ let left = 0 ;
27+ let right = string . length - 1 ;
28+
29+ while ( left < right ) {
30+ if ( string [ left ] !== string [ right ] ) {
31+ return false ;
32+ } else {
33+ left ++ ;
34+ right -- ;
35+ }
36+ }
37+ return true ;
38+ } ;
You can’t perform that action at this time.
0 commit comments