New Problem Solution - "Minimum Length of String After Deleting Similar Ends"
This commit is contained in:
parent
ee9962f201
commit
b74969f4ab
@ -12,6 +12,7 @@ LeetCode
|
||||
|1754|[Largest Merge Of Two Strings](https://leetcode.com/problems/largest-merge-of-two-strings/) | [C++](./algorithms/cpp/largestMergeOfTwoStrings/LargestMergeOfTwoStrings.cpp)|Medium|
|
||||
|1753|[Maximum Score From Removing Stones](https://leetcode.com/problems/maximum-score-from-removing-stones/) | [C++](./algorithms/cpp/maximumScoreFromRemovingStones/MaximumScoreFromRemovingStones.cpp)|Medium|
|
||||
|1752|[Check if Array Is Sorted and Rotated](https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/) | [C++](./algorithms/cpp/checkIfArrayIsSortedAndRotated/CheckIfArrayIsSortedAndRotated.cpp)|Easy|
|
||||
|1750|[Minimum Length of String After Deleting Similar Ends](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | [C++](./algorithms/cpp/minimumLengthOfStringAfterDeletingSimilarEnds/MinimumLengthOfStringAfterDeletingSimilarEnds.cpp)|Medium|
|
||||
|1749|[Maximum Absolute Sum of Any Subarray](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/) | [C++](./algorithms/cpp/maximumAbsoluteSumOfAnySubarray/MaximumAbsoluteSumOfAnySubarray.cpp)|Medium|
|
||||
|1748|[Sum of Unique Elements](https://leetcode.com/problems/sum-of-unique-elements/) | [C++](./algorithms/cpp/sumOfUniqueElements/SumOfUniqueElements.cpp)|Easy|
|
||||
|1605|[Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp)|Medium|
|
||||
|
@ -0,0 +1,68 @@
|
||||
// Source : https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/
|
||||
// Author : Hao Chen
|
||||
// Date : 2021-02-12
|
||||
|
||||
/*****************************************************************************************************
|
||||
*
|
||||
* 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'.
|
||||
******************************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int minimumLength(string s) {
|
||||
char ch;
|
||||
int left=0, right=s.size()-1;
|
||||
|
||||
while(left < right) {
|
||||
ch = s[left];
|
||||
if (s[right] != ch) break;
|
||||
|
||||
while (s[left] == ch) left++;
|
||||
|
||||
if (left >= right ) return 0;
|
||||
while (s[right] == ch) right--;
|
||||
}
|
||||
|
||||
return right - left + 1;
|
||||
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user