2024-03-05 Daily Challenge
Today I have done leetcode's March LeetCoding Challenge with cpp
.
March LeetCoding Challenge 5
Description
Minimum Length of String After Deleting Similar Ends
Given a string s
consisting only of characters 'a'
, 'b'
, and 'c'
. You are asked to apply the following algorithm on the string any number of times:
- Pick a non-empty prefix from the string
s
where all the characters in the prefix are equal. - Pick a non-empty suffix from the string
s
where all the characters in this suffix are equal. - The prefix and the suffix should not intersect at any index.
- The characters from the prefix and suffix must be the same.
- Delete both the prefix and the suffix.
Return the minimum length of s
after performing the above operation any number of times (possibly zero times).
Example 1:
Input: s = "ca" Output: 2 Explanation: You can't remove any characters, so the string stays as is.
Example 2:
Input: s = "cabaabac" Output: 0 Explanation: An optimal sequence of operations is: - Take prefix = "c" and suffix = "c" and remove them, s = "abaaba". - Take prefix = "a" and suffix = "a" and remove them, s = "baab". - Take prefix = "b" and suffix = "b" and remove them, s = "aa". - Take prefix = "a" and suffix = "a" and remove them, s = "".
Example 3:
Input: s = "aabccabba" Output: 3 Explanation: An optimal sequence of operations is: - Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb". - Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
Constraints:
1 <= s.length <= 105
s
only consists of characters'a'
,'b'
, and'c'
.
Solution
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
public:
int minimumLength(string s) {
int len = s.length();
int begin = 0;
int end = len-1;
while(begin < end && s[begin] == s[end]) {
char c = s[begin];
begin += 1;
end -= 1;
while(begin < end && s[begin] == c) {
begin += 1;
}
while(begin <= end && s[end] == c) {
end -= 1;
}
}
return end - begin + 1;
}
};
// Accepted
// 100/100 cases passed (15 ms)
// Your runtime beats 99.31 % of cpp submissions
// Your memory usage beats 13.45 % of cpp submissions (14 MB)