Skip to content

Commit 25424e7

Browse files
committed
Merge branch 'master' of github.com:BaffinLee/leetcode-javascript
2 parents cd0de6a + 7e8e39b commit 25424e7

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

001-100/5. Longest Palindromic Substring.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,38 @@ var expandAroundCenter = function (s, left, right) {
6565

6666
* Time complexity : O(n^2).
6767
* Space complexity : O(1).
68+
69+
70+
```js
71+
/**
72+
* @param {string} s
73+
* @return {string}
74+
*/
75+
var longestPalindrome = function(s) {
76+
let startIndex = 0;
77+
let maxLength = 1;
78+
79+
function expandAroundCenter(left, right) {
80+
while (left >=0 && right < s.length && s[left] === s[right]) {
81+
const currentPalLength = right - left + 1;
82+
if (currentPalLength > maxLength) {
83+
maxLength = currentPalLength;
84+
startIndex = left;
85+
}
86+
left -= 1;
87+
right += 1;
88+
}
89+
}
90+
91+
for (let i = 0; i < s.length; i++) {
92+
expandAroundCenter(i-1, i+1);
93+
expandAroundCenter(i, i+1);
94+
}
95+
96+
return s.slice(startIndex, startIndex + maxLength)
97+
};
98+
```
99+
**Complexity:**
100+
101+
* Time complexity : O(n^2).
102+
* Space complexity : O(1).

101-200/125. Valid Palindrome.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,34 @@ nope.
6161

6262
* Time complexity : O(n).
6363
* Space complexity : O(n).
64+
65+
66+
```js
67+
/**
68+
* @param {string} s
69+
* @return {boolean}
70+
*/
71+
var isPalindrome = function(s) {
72+
73+
s = s.toLowerCase().replace(/[\W_]/g, '');
74+
75+
let left = 0;
76+
let right = s.length - 1;
77+
78+
while (left < right) {
79+
if (s[left] !== s[right]) {
80+
return false
81+
}
82+
left++;
83+
right--;
84+
}
85+
86+
return true;
87+
88+
};
89+
```
90+
91+
**Complexity:**
92+
93+
* Time complexity : O(n).
94+
* Space complexity : O(1). left and right pointers take the constant space.

0 commit comments

Comments
 (0)