Skip to content

feat(algorithms, stack): min str length after removing substrings#177

Merged
BrianLusina merged 7 commits intomainfrom
feat/algorithms-stack-min-str-len
Feb 16, 2026
Merged

feat(algorithms, stack): min str length after removing substrings#177
BrianLusina merged 7 commits intomainfrom
feat/algorithms-stack-min-str-len

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Feb 16, 2026

Describe your change:

Minimum String length after removing substrings

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added solutions for "Magnetic Force Between Two Balls" and three approaches for "Minimum String Length After Removing Substrings."
  • Documentation

    • Detailed problem write-ups added for both problems.
    • Directory index updated with new entries including Bloom Filter and Longest Word With Prefixes (duplicate entries mirrored in multiple sections).
  • Tests

    • New test suites added to validate the introduced solutions.

@BrianLusina BrianLusina self-assigned this Feb 16, 2026
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Documentation Documentation Updates Strings Stack Stack data structure In Place Manipulation labels Feb 16, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 16, 2026

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds new algorithm implementations and documentation: "Magnetic Force Between Two Balls" (binary search), "Minimum String Length After Removing Substrings" (three approaches), corresponding unit tests, and additional DIRECTORY.md entries including Bloom Filter links (some duplicated placements).

Changes

Cohort / File(s) Summary
Index / Directory
DIRECTORY.md
Added entries/links for Magnetic Force Between Two Balls, Minimum String Length After Removing Substrings, and Bloom Filter in multiple sections (duplicate placements).
Magnetic Force Between Two Balls
algorithms/search/binary_search/magnetic_force_between_two_balls/README.md, algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py, algorithms/search/binary_search/magnetic_force_between_two_balls/test_magnetic_force_between_two_balls.py
New problem README; added max_distance(position: List[int], m: int) implementing binary search + greedy placement; added parameterized unit tests.
Minimum String Length After Removing Substrings
algorithms/stack/minimum_string_length_after_removing_substrings/README.md, algorithms/stack/minimum_string_length_after_removing_substrings/__init__.py, algorithms/stack/minimum_string_length_after_removing_substrings/test_min_str_length_after_removing_substrings.py
Added problem README and three implementations exported (min_length_stack, min_length_string_replace, min_length_in_place_manipulation) with tests validating each approach.

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Suggested labels

Datastructures

Poem

🐰 I hopped along the code-lined trail,
Found balls and stacks and a bloom-filter tale,
I nudged some tests and polished docs bright,
Then vanished to my burrow before midnight,
A happy rabbit, coding in moonlight.

🚥 Pre-merge checks | ✅ 2 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description indicates multiple algorithm files were added (minimum string length AND magnetic force), contradicting the stated checklist item that 'This PR only changes one algorithm file.' Clarify whether this PR should include both algorithms or split into separate PRs per repository convention. Update description and checklist items to accurately reflect all changes included.
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: adding a minimum string length algorithm in the stack domain, directly matching the primary contribution of this PR.
Merge Conflict Detection ✅ Passed ✅ No merge conflicts detected when merging into main

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/algorithms-stack-min-str-len

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In
`@algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py`:
- Around line 17-19: The module/function docstring that begins "Check if we can
place 'm' balls at 'position' with each ball having at least 'x' gap." has a
stray quote after the opening triple-quote (it is currently """ "), causing a
malformed docstring; remove the extra double-quote so the docstring starts with
""" and ensure the closing triple-quote remains intact (locate the docstring for
the helper that checks placement of m balls and fix the opening delimiter).

In `@algorithms/search/binary_search/magnetic_force_between_two_balls/README.md`:
- Around line 51-52: The second bullet point is inverted; change the sentence
that begins "Similarly if you cannot place balls such that the answer is x then
you can do it for y where y > x." to state the correct monotonicity: "If you
cannot place balls such that the answer is x then you cannot do it for y where y
> x." Keep the first bullet ("If you can place balls such that the answer is x
then you can do it for y where y < x.") as-is so the README explains the
monotonic property correctly.

In `@algorithms/stack/minimum_string_length_after_removing_substrings/README.md`:
- Around line 180-182: Fix the broken image reference in README.md: replace the
duplicate second image path that currently points to
"minimum_string_length_after_removing_substrings_in-place_solution_1.png" and
missing "./" with the correct
"./images/solutions/minimum_string_length_after_removing_substrings_in-place_solution_2.png"
so Solution 2 uses the distinct _solution_2.png filename and consistent "./"
prefix like the other image links; leave Solution 1 and Solution 3 lines
unchanged.
🧹 Nitpick comments (2)
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py (1)

13-14: Upper bound uses sorted_positions[-1] instead of the range.

The high bound should be based on (sorted_positions[-1] - sorted_positions[0]) // (m - 1) + 1 rather than sorted_positions[-1] / (m - 1). The current formula works correctly but causes unnecessary binary search iterations when the minimum position is large.

♻️ Suggested improvement
-    high = int(sorted_positions[-1] / (m - 1.0)) + 1
+    high = (sorted_positions[-1] - sorted_positions[0]) // (m - 1) + 1
algorithms/stack/minimum_string_length_after_removing_substrings/__init__.py (1)

27-34: Minor inefficiency: redundant in checks.

The while condition already checks for "AB" in s, but the if on line 29 checks again. You could simplify slightly, though this doesn't affect correctness.

♻️ Optional simplification
 def min_length_string_replace(s: str) -> int:
-    while "AB" in s or "CD" in s:
-        if "AB" in s:
-            s = s.replace("AB", "")
-        elif "CD" in s:
-            s = s.replace("CD", "")
+    while "AB" in s or "CD" in s:
+        s = s.replace("AB", "").replace("CD", "")
 
     return len(s)

BrianLusina and others added 3 commits February 16, 2026 14:07
…gs/README.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/__init__.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/README.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Fix all issues with AI agents
In
`@algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py`:
- Around line 4-14: The function max_distance can raise ZeroDivisionError when m
== 1 and lacks validation for m <= 0; update max_distance to handle edge cases
up front: if m <= 0 raise a ValueError (or handle per project conventions), and
if m == 1 return 0 (or the correct value for placing one ball) before computing
high; then compute high using (m - 1.0) safely only when m > 1, keeping the rest
of the binary-search logic unchanged and referencing variables sorted_positions,
low, high, result, n within max_distance.

In `@algorithms/stack/minimum_string_length_after_removing_substrings/README.md`:
- Line 218: Update the C++ space complexity note sentence that currently reads
"String are mutable in C++." to correct grammar by changing it to "Strings are
mutable in C++." in the README entry for the
minimum_string_length_after_removing_substrings algorithm; ensure the rest of
the sentence ("So, all modifications can be done on the input string itself. No
additional data structures") remains unchanged and punctuation is consistent.
- Around line 24-26: Example 4's step descriptions incorrectly claim whole
strings ("ABFCACDB", "FCACDB", "FCAB") are removed; update the three steps so
each operation removes only an "AB" or "CD" substring as allowed by the problem
and update the resulting s after each removal accordingly (i.e., reference
Example 4 and replace the current removals of "ABFCACDB", "FCACDB", "FCAB" with
the correct sequence of removing "AB" or "CD" and the correct intermediate
strings).
🧹 Nitpick comments (1)
algorithms/search/binary_search/magnetic_force_between_two_balls/__init__.py (1)

14-14: Upper bound can be tightened.

The search space upper bound uses sorted_positions[-1] (the absolute max value) instead of the range sorted_positions[-1] - sorted_positions[0]. The current bound is correct but looser than necessary, resulting in a few extra binary search iterations.

♻️ Suggested tighter bound
-    high = int(sorted_positions[-1] / (m - 1.0)) + 1
+    high = int((sorted_positions[-1] - sorted_positions[0]) / (m - 1.0)) + 1

BrianLusina and others added 2 commits February 16, 2026 16:55
…gs/README.md

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
…ls/__init__.py

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@BrianLusina BrianLusina merged commit ef8af1c into main Feb 16, 2026
4 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Documentation Documentation Updates enhancement In Place Manipulation Stack Stack data structure Strings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant