Skip to content

leetcode/868: pigeonhole principle#380

Merged
tony84727 merged 2 commits intomainfrom
leetcode/1461
Feb 23, 2026
Merged

leetcode/868: pigeonhole principle#380
tony84727 merged 2 commits intomainfrom
leetcode/1461

Conversation

@tony84727
Copy link
Copy Markdown
Owner

No description provided.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @tony84727, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new Rust solution for a LeetCode problem, focusing on efficiently identifying the presence of all possible binary codes of a given length within a string. The changes include the core algorithm implementation, associated documentation, and updates to the module structure to incorporate the new functionality and maintain organization.

Highlights

  • New LeetCode Solution: Implemented a Rust solution for LeetCode problem 1461, "Check If a String Contains All Binary Codes of Size K", utilizing a sliding window approach with a HashSet to efficiently track unique binary substrings.
  • Documentation Added: A new README.md file was added for algorithm_1461, providing the problem title and a direct link to its LeetCode page.
  • Module Integration: The new algorithm_1461 module was integrated into the main src/leetcode/mod.rs file, making it accessible within the project structure.
  • Module Refactoring: The src/leetcode/mod.rs file was refactored to relocate the algorithm_693 module entry to maintain alphabetical order or logical grouping.
Changelog
  • src/leetcode/algorithm_1461/README.md
    • Added a new file containing the problem title and a link to the LeetCode problem.
  • src/leetcode/algorithm_1461/mod.rs
    • Added the Rust solution for LeetCode problem 1461, "Check If a String Contains All Binary Codes of Size K".
    • Implemented the has_all_codes function using a HashSet and a sliding window approach to find all unique binary substrings.
    • Included unit tests for the implemented solution to verify correctness.
  • src/leetcode/mod.rs
    • Added a pub mod declaration for algorithm_1461 to expose the new module.
    • Relocated the pub mod declaration for algorithm_693 to a different section of the file.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a Rust solution for the LeetCode problem "Check If a String Contains All Binary Codes of Size K". While the solution is well-implemented, utilizing a HashSet and efficient bit manipulation with a sliding window approach, it currently lacks input validation for the parameters s and k. This omission could lead to Denial of Service (DoS) via memory exhaustion (OOM) or application panics (integer underflow/overflowing shifts), specifically if k is excessively large or s is too short relative to k. Unit tests are provided, and the README.md and src/leetcode/mod.rs updates are appropriate.

Comment on lines +7 to +12
let k = k as usize;
let goal = 1 << k;
if s.len() - k + 1 < goal {
return false;
}
let mut codes = HashSet::with_capacity(goal);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-high high

The code is vulnerable to Denial of Service (DoS) in several ways:

  1. Out-of-Memory (OOM): HashSet::with_capacity(goal) where goal = 1 << k can attempt to allocate a massive amount of memory if k is large.
  2. Integer Underflow: s.len() - k will panic if s.len() < k.
  3. Overflowing Shift: 1 << k and other shifts will panic if k is 32 or greater (for i32) or 64 or greater (for usize on 64-bit systems).

Validation should be added to ensure k is within a reasonable range (e.g., 1 to 20 as per the problem constraints) and that s.len() >= k before performing subtraction.

Suggested change
let k = k as usize;
let goal = 1 << k;
if s.len() - k + 1 < goal {
return false;
}
let mut codes = HashSet::with_capacity(goal);
if k < 1 || k > 20 {
return false;
}
let k = k as usize;
let goal = 1 << k;
if s.len() < k || s.len() - k + 1 < goal {
return false;
}
let mut codes = HashSet::with_capacity(goal);

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5a8c14cba1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/leetcode/algorithm_1461/mod.rs Outdated
pub fn has_all_codes(s: String, k: i32) -> bool {
let k = k as usize;
let goal = 1 << k;
if s.len() - k + 1 < goal {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Guard length check when k exceeds input length

The condition s.len() - k + 1 < goal underflows when k > s.len(), so valid inputs like s = "0", k = 2 panic in debug builds instead of returning false. This path is reachable before any early return, which makes the implementation crash on an expected edge case; add an explicit s.len() < k guard (or checked arithmetic) before performing the subtraction.

Useful? React with 👍 / 👎.

@tony84727 tony84727 merged commit 82bd100 into main Feb 23, 2026
4 checks passed
@tony84727 tony84727 deleted the leetcode/1461 branch February 23, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant