-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minimum Length of String After Deleting Similar Ends
- Loading branch information
Showing
5 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
solutions/minimum-length-of-string-after-deleting-similar-ends/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
23 changes: 23 additions & 0 deletions
23
solutions/minimum-length-of-string-after-deleting-similar-ends/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
30 changes: 30 additions & 0 deletions
30
solutions/minimum-length-of-string-after-deleting-similar-ends/test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |