Skip to content

feat: add solutions for lc No.0645,0647#5117

Merged
yanglbme merged 1 commit intomainfrom
dev
Mar 27, 2026
Merged

feat: add solutions for lc No.0645,0647#5117
yanglbme merged 1 commit intomainfrom
dev

Conversation

@yanglbme
Copy link
Copy Markdown
Member

No description provided.

Copilot AI review requested due to automatic review settings March 27, 2026 11:43
@idoocs idoocs added core team Issues or pull requests from core team cpp Issues or Pull requests relate to .cpp code go Issues or Pull requests relate to .go code md Issues or Pull requests relate to .md files rs Issues or Pull requests relate to .rs code ts Issues or Pull requests relate to .ts code labels Mar 27, 2026
@yanglbme yanglbme merged commit 5692785 into main Mar 27, 2026
14 of 15 checks passed
@yanglbme yanglbme deleted the dev branch March 27, 2026 11:43
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds additional solutions for LeetCode 0647 (Palindromic Substrings) and updates 0645 (Set Mismatch) Rust solutions and documentation.

Changes:

  • Added Manacher’s Algorithm implementations for 0647 in C++, Go, and TypeScript (plus README updates describing both approaches).
  • Updated 0645 Rust “Solution2” to a counting/HashMap approach and added a separate XOR-based “Solution3”.
  • Expanded README explanations for both problems (EN/ZH), including additional language snippets.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
solution/0600-0699/0647.Palindromic Substrings/Solution2.ts Adds Manacher’s Algorithm solution in TypeScript
solution/0600-0699/0647.Palindromic Substrings/Solution2.go Adds Manacher’s Algorithm solution in Go
solution/0600-0699/0647.Palindromic Substrings/Solution2.cpp Adds Manacher’s Algorithm solution in C++
solution/0600-0699/0647.Palindromic Substrings/README_EN.md Adds explanations for Solution 1/2 and includes C++/Go/TS snippets for Manacher
solution/0600-0699/0647.Palindromic Substrings/README.md Expands Chinese explanation and adds C++/Go/TS snippets for Manacher
solution/0600-0699/0645.Set Mismatch/Solution2.rs Replaces previous approach with HashMap counting approach
solution/0600-0699/0645.Set Mismatch/Solution3.rs Adds XOR-based approach as an additional Rust solution
solution/0600-0699/0645.Set Mismatch/README_EN.md Updates Rust snippet for one solution and adds another Rust snippet for XOR approach
solution/0600-0699/0645.Set Mismatch/README.md Same as README_EN.md, in Chinese

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +2 to +7
t := "^#"
for _, c := range s {
t += string(c)
t += "#"
}
t += "$"
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Building t via repeated string concatenation inside the loop is O(n^2) due to repeated allocations/copies, which undermines Manacher’s intended O(n) runtime. Consider building t with a preallocated buffer (e.g., []byte with capacity 2*len(s)+3) or a strings.Builder, then operate on bytes to keep construction O(n).

Suggested change
t := "^#"
for _, c := range s {
t += string(c)
t += "#"
}
t += "$"
buf := make([]rune, 0, 2*len(s)+3)
buf = append(buf, '^', '#')
for _, c := range s {
buf = append(buf, c, '#')
}
buf = append(buf, '$')
t := string(buf)

Copilot uses AI. Check for mistakes.
Comment on lines +2 to +7
let t = "^#";
for (const c of s) {
t += c + "#";
}
t += "$";

Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeated += concatenation in a loop can devolve into O(n^2) behavior because strings are immutable and may be repeatedly copied. To preserve the intended O(n) preprocessing, consider accumulating pieces in an array and join(''), or otherwise building t with a linear-time strategy.

Suggested change
let t = "^#";
for (const c of s) {
t += c + "#";
}
t += "$";
const parts: string[] = ["^", "#"];
for (const c of s) {
parts.push(c, "#");
}
parts.push("$");
const t = parts.join("");

Copilot uses AI. Check for mistakes.
### Solution 2
### Solution 2: Manacher's Algorithm

In Manacher's algorithm, $p[i] - 1$ represents the maximum palindrome length centered at position $i$, and the number of palindromic substrings centered at position $i$ is $\left \lceil \frac{p[i]-1}{2} \right \rceil$.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The wording “maximum palindrome length” is ambiguous here: in this implementation p[i] is the radius in the transformed string (with separators), not a direct length in the original string. Consider clarifying that p[i]-1 is the radius/half-length in the transformed string and explicitly relating it to the count formula used in code (e.g., floor(p[i]/2)), to avoid confusion for readers.

Suggested change
In Manacher's algorithm, $p[i] - 1$ represents the maximum palindrome length centered at position $i$, and the number of palindromic substrings centered at position $i$ is $\left \lceil \frac{p[i]-1}{2} \right \rceil$.
In this implementation of Manacher's algorithm, $p[i]$ is the palindrome radius in the transformed string (with separators). Thus $p[i] - 1$ is the maximum palindrome radius around position $i$ in the transformed string (excluding the first mismatching characters), and the number of palindromic substrings in the original string centered at position $i$ is $\left\lfloor \frac{p[i]}{2} \right\rfloor$, which is equal to $\left\lceil \frac{p[i]-1}{2} \right\rceil$.

Copilot uses AI. Check for mistakes.
@@ -166,7 +168,7 @@ var countSubstrings = function (s) {

在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$。
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的“最大回文长度”表述容易引起歧义:本实现里 p[i] 是在插入分隔符后的变换字符串上的回文半径(radius),不等同于原字符串中的长度。建议注明这是变换字符串上的半径/扩展长度,并说明与代码里 ans += p[i] / 2(或 floor(p[i]/2))的对应关系,避免读者误解。

Suggested change
在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$
在 Manacher 算法中,我们先构造插入分隔符后的变换字符串 `t`,此时 `p[i]` 表示在字符串 `t` 上以第 `i` 位为中心的回文半径(radius,至少为 1,单位是「变换串中的字符数」)。对应到原字符串上,以第 `i` 位为中心所能产生的回文子串数量正好是 `p[i] // 2`,这也是代码中 `ans += p[i] // 2` 的含义

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core team Issues or pull requests from core team cpp Issues or Pull requests relate to .cpp code go Issues or Pull requests relate to .go code md Issues or Pull requests relate to .md files rs Issues or Pull requests relate to .rs code ts Issues or Pull requests relate to .ts code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants