Skip to content

Commit

Permalink
Minimum Length of String After Deleting Similar Ends
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Mar 5, 2024
1 parent bdda048 commit 4075ae4
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ add_task(minimum-index-sum-of-two-lists)
add_task(minimum-insertion-steps-to-make-a-string-palindrome)
add_task(minimum-jumps-to-reach-home)
add_task(minimum-knight-moves)
add_task(minimum-length-of-string-after-deleting-similar-ends)
add_task(minimum-limit-of-balls-in-a-bag)
add_task(minimum-moves-to-capture-the-queen)
add_task(minimum-moves-to-convert-string)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,7 @@
| 1745. | [Palindrome Partitioning IV](https://leetcode.com/problems/palindrome-partitioning-iv/) | [C++](./solutions/palindrome-partitioning-iv/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> | |
| 1747. | [Leetflex Banned Accounts 🔒](https://leetcode.com/problems/leetflex-banned-accounts/) | [SQL](./extra/sql/leetflex-banned-accounts/solution.sql), [PY](./extra/pandas/leetflex-banned-accounts/solution.py) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Database-337c7f?style=flat-square'/> | |
| 1748. | [Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [C++](./solutions/sum-of-unique-elements/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 1750. | [Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | [C++](./solutions/minimum-length-of-string-after-deleting-similar-ends/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 1751. | [Maximum Number of Events That Can Be Attended II](https://leetcode.com/problems/maximum-number-of-events-that-can-be-attended-ii/) | [C++](./solutions/maximum-number-of-events-that-can-be-attended-ii/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Search-33557f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1752. | [Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C++](./solutions/check-if-array-is-sorted-and-rotated/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> | |
| 1753. | [Maximum Score From Removing Stones](https://leetcode.com/problems/maximum-score-from-removing-stones/) | [C++](./solutions/maximum-score-from-removing-stones/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(1) / O(1)| <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> | |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_minimum_length_of_string_after_deleting_similar_ends test.cpp)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <string>

// Time: O(N)
// Space: O(1)

class Solution {
public:
static int minimumLength(std::string s) {
int l = 0, r = s.size() - 1;
while (l < r && s[l] == s[r]) {
const auto c = s[l];
while (l <= r && s[l] == c) {
++l;
}
while (l <= r && s[r] == c) {
--r;
}
}
return r - l + 1;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::string s;
int expected;
};

std::vector<TestCase> test_cases{
{
.s = "ca",
.expected = 2,
},
{
.s = "cabaabac",
.expected = 0,
},
{
.s = "aabccabba",
.expected = 3,
},
};

for (const auto &[s, expected] : test_cases) {
const auto actual = Solution::minimumLength(s);
REQUIRE(expected == actual);
}
}

0 comments on commit 4075ae4

Please sign in to comment.