forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimumLengthOfStringAfterDeletingSimilarEnds.cpp
68 lines (61 loc) · 2.25 KB
/
MinimumLengthOfStringAfterDeletingSimilarEnds.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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;
}
};