Skip to content

Commit

Permalink
refactor(string): move 680-valid-palindrome-ii from greedy/ to string/
Browse files Browse the repository at this point in the history
  • Loading branch information
RayJune committed Jul 26, 2024
1 parent d28aef5 commit af26965
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/string/125-valid-palindrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* s consists only of printable ASCII characters.
*
* https://leetcode.com/problems/valid-palindrome/
*
* @related 680-valid-palindrome-ii
*/

/**
Expand All @@ -32,10 +34,8 @@
* 3. 指定空字符串为回文字符串
*
* 思路:
* 1. 大小写统一,用 .toLowerCase 处理
* 2. 正则匹配,筛选出由字母和数字所组成的字符串
* 3. 字符串反转,.split + .reverse + .join 方法
* 4. 将反转字符串与原字符串作比较得出结果
* 1. 统一大小写,正则匹配出字符串中的字母和数字,得到目标字符串
* 2. 将目标字符串反转,拿反转后的字符串与目标字符串作比较得出是否是回文字符串
*
* Time Complexity: O(n) = 遍历次数
* Space Complexity: O(n) = 创建的数组长度 / 创建的字符串长度
Expand Down
12 changes: 6 additions & 6 deletions src/string/125-valid-palindrome/optimize.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@
* s consists only of printable ASCII characters.
*
* https://leetcode.com/problems/valid-palindrome/
*
* @related 680-valid-palindrome-ii
*/

/**
* 输入一个字符串,判断它是否是回文字符串
* 注意:
* 1. 只考虑字幕和数字
* 2. 忽略字母的大小写
* 3. 指定空字符串为回文字符串
* 1. 只考虑数字和字母,忽略字母的大小写
* 2. 定义空字符串为回文字符串
*
* 思路:
* 1. 统一大小写,.toLowerCase
* 2. 正则匹配筛选出字母和数字组成的字符串
* 3. 遍历筛选后的字符串,利用 s[i] === s[len - i - 1] 来判断是否是回文字符串
* 1. 统一大小写,正则匹配出字符串中的字母和数字,得到目标字符串
* 2. 遍历目标字符串,判断 s[i] === s[s.length - i - 1] 来得出是否是回文字符串
*
* Time Complexity: O(n) = 遍历次数
* Space Complexity: O(n) = 创建的字符串长度
Expand Down
6 changes: 4 additions & 2 deletions src/string/125-valid-palindrome/optimize2.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* s consists only of printable ASCII characters.
*
* https://leetcode.com/problems/valid-palindrome/
*
* @related 680-valid-palindrome-ii
*/

/**
Expand All @@ -34,8 +36,8 @@
* 思路:
* Two Pointers 双指针,原地做比较,不创建新的字符串、数组
* 1. 创建 left right 两个下标,分别代表要比较的前后对称两个值的下标
* 2. 遍历字符串 s,以此判断两个下标对应的值是否是字母或数字,如果不是则把对应下标向前/向后移动,直到为字母或数字值为止
* 3. 依次判断 left right 对应的值
* 2. 遍历字符串 s,依次判断两个下标对应的值是否是字母或数字,如果不是则把对应下标向前/向后移动,直到值为字母或数字为止
* 3. 依次判断 s[left].toLowerCase === s[right].toLowerCase()
*
* Time Complexity: O(n) = 遍历次数
* Space Complexity: O(1)
Expand Down
2 changes: 2 additions & 0 deletions src/string/125-valid-palindrome/template-zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* s consists only of printable ASCII characters.
*
* https://leetcode.cn/problems/valid-palindrome/
*
* @related 680-valid-palindrome-ii
*/

module.exports = isPalindrome;
2 changes: 2 additions & 0 deletions src/string/125-valid-palindrome/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
* s consists only of printable ASCII characters.
*
* https://leetcode.com/problems/valid-palindrome/
*
* @related 680-valid-palindrome-ii
*/

module.exports = isPalindrome;
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@
* s consists of lowercase English letters.
*
* https://leetcode.com/problems/valid-palindrome-ii/
*
* @related 125-valid-palindrome
*/


/**
* Greedy Algorithm
* 给定一个非空字符串 s,最多删除一个字符,判断能否成为回文字符串
*
* 思路:
* 贪心算法 Greedy Algorithm
* 定义 left right 两个初始值为头尾的指针,依次判断 s[left] === s[right]
* 如果相同则 left += 1 right -= 1
* 如果不同则分别判断去除 s[left] 和 s[right] 后的子字符串是否是回文字符串
*
* Time Complexity: O(n) = while 循环次数 / isPalindrome 操作
* Space complexity: O(n)
* Auxiliary complexity: O(n)
* Time Complexity: O(n) = 遍历次数
* Space complexity: O(1)
* Auxiliary complexity: O(1)
* 其中 n 是字符串 s 的长度
*
* @param {string} s
* @returns {boolean}
Expand All @@ -39,7 +49,7 @@ function validPalindrome(s) {

while (left < right) {
if (s[left] !== s[right]) {
return isPalindrome(s, left + 1, right) || isPalindrome(s, left, right - 1);
return isPalindrome(left + 1, right, s) || isPalindrome(left, right - 1, s);
}
left += 1;
right -= 1;
Expand All @@ -49,16 +59,17 @@ function validPalindrome(s) {
}

/**
* Time Complexity: O(n) = while 循环次数
* Time Complexity: O(n) = 遍历次数
* Space complexity: O(1)
* Auxiliary complexity: O(1)
* 其中 n 是字符串 s 的长度
*
* @param {string} s
* @param {left} left
* @param {right} right
* @returns {boolean}
*/
function isPalindrome(s, left, right) {
function isPalindrome(left, right, s) {
while (left < right) {
if (s[left] !== s[right]) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* s consists of lowercase English letters.
*
* https://leetcode.cn/problems/valid-palindrome-ii/
*
* @related 125-valid-palindrome
*/

module.exports = validPalindrome;
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* s consists of lowercase English letters.
*
* https://leetcode.com/problems/valid-palindrome-ii/
*
* @related 125-valid-palindrome
*/

module.exports = validPalindrome;
File renamed without changes.

0 comments on commit af26965

Please sign in to comment.