Skip to content

Commit 3a0a6b5

Browse files
Strings. Valid Palindrome.
1 parent a432e47 commit 3a0a6b5

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
2. [Reverse Integer](./Strings/reverse-integer.js)
1919
3. [First Unique Character in a String](./Strings/first-unique-character-in-a-string.js)
2020
4. [Valid Anagram](./Strings/valid-anagram.js)
21+
5. [Valid Palindrome](./Strings/valid-palindrome.js)

Strings/valid-palindrome.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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-9a-z]/gi, "").toLowerCase();
7+
const reverseString = s
8+
.replace(/[^0-9a-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-9a-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+
};

0 commit comments

Comments
 (0)