Conversation
There was a problem hiding this comment.
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.
| t := "^#" | ||
| for _, c := range s { | ||
| t += string(c) | ||
| t += "#" | ||
| } | ||
| t += "$" |
There was a problem hiding this comment.
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).
| 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) |
| let t = "^#"; | ||
| for (const c of s) { | ||
| t += c + "#"; | ||
| } | ||
| t += "$"; | ||
|
|
There was a problem hiding this comment.
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.
| 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(""); |
| ### 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$. |
There was a problem hiding this comment.
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.
| 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$. |
| @@ -166,7 +168,7 @@ var countSubstrings = function (s) { | |||
|
|
|||
| 在 Manacher 算法的计算过程中,用 $p[i]-1$ 表示以第 $i$ 位为中心的最大回文长度,以第 $i$ 位为中心的回文串数量为 $\left \lceil \frac{p[i]-1}{2} \right \rceil$。 | |||
There was a problem hiding this comment.
这里的“最大回文长度”表述容易引起歧义:本实现里 p[i] 是在插入分隔符后的变换字符串上的回文半径(radius),不等同于原字符串中的长度。建议注明这是变换字符串上的半径/扩展长度,并说明与代码里 ans += p[i] / 2(或 floor(p[i]/2))的对应关系,避免读者误解。
| 在 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` 的含义。 |
No description provided.