|
| 1 | +// Source : https://leetcode.com/problems/maximum-score-from-removing-substrings/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-28 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You are given a string s and two integers x and y. You can perform two types of operations any |
| 8 | + * number of times. |
| 9 | + * |
| 10 | + * Remove substring "ab" and gain x points. |
| 11 | + * |
| 12 | + * For example, when removing "ab" from "cabxbae" it becomes "cxbae". |
| 13 | + * |
| 14 | + * Remove substring "ba" and gain y points. |
| 15 | + * |
| 16 | + * For example, when removing "ba" from "cabxbae" it becomes "cabxe". |
| 17 | + * |
| 18 | + * Return the maximum points you can gain after applying the above operations on s. |
| 19 | + * |
| 20 | + * Example 1: |
| 21 | + * |
| 22 | + * Input: s = "cdbcbbaaabab", x = 4, y = 5 |
| 23 | + * Output: 19 |
| 24 | + * Explanation: |
| 25 | + * - Remove the "ba" underlined in "cdbcbbaaabab". Now, s = "cdbcbbaaab" and 5 points are added to the |
| 26 | + * score. |
| 27 | + * - Remove the "ab" underlined in "cdbcbbaaab". Now, s = "cdbcbbaa" and 4 points are added to the |
| 28 | + * score. |
| 29 | + * - Remove the "ba" underlined in "cdbcbbaa". Now, s = "cdbcba" and 5 points are added to the score. |
| 30 | + * - Remove the "ba" underlined in "cdbcba". Now, s = "cdbc" and 5 points are added to the score. |
| 31 | + * Total score = 5 + 4 + 5 + 5 = 19. |
| 32 | + * |
| 33 | + * Example 2: |
| 34 | + * |
| 35 | + * Input: s = "aabbaaxybbaabb", x = 5, y = 4 |
| 36 | + * Output: 20 |
| 37 | + * |
| 38 | + * Constraints: |
| 39 | + * |
| 40 | + * 1 <= s.length <= 10^5 |
| 41 | + * 1 <= x, y <= 10^4 |
| 42 | + * s consists of lowercase English letters. |
| 43 | + ******************************************************************************************************/ |
| 44 | + |
| 45 | +class Solution { |
| 46 | +public: |
| 47 | + int maximumGain(string s, int x, int y) { |
| 48 | + char key[] ="ab"; |
| 49 | + if (y > x) { key[0] = 'b'; key[1]='a';} |
| 50 | + |
| 51 | + int high = max(x,y); |
| 52 | + int low = min(x,y); |
| 53 | + |
| 54 | + //greedy for high score |
| 55 | + int score = 0; |
| 56 | + stack<char> left_stack; |
| 57 | + for (int i=0; i<s.size(); i++) { |
| 58 | + char c = s[i]; |
| 59 | + if ( left_stack.empty() || //stack is empty, just push directly |
| 60 | + ( c != key[0] && c != key[1] ) ) { // not the score char, just tpush cirectory |
| 61 | + left_stack.push(c); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // if we meet the high score pattern |
| 66 | + if ( c == key[1] && left_stack.top() == key[0]){ |
| 67 | + //cout << key << endl; |
| 68 | + left_stack.pop(); |
| 69 | + score += high; |
| 70 | + continue; |
| 71 | + } |
| 72 | + left_stack.push(c); |
| 73 | + } |
| 74 | + |
| 75 | + //process the low score |
| 76 | + stack<char> right_stack; |
| 77 | + while(!left_stack.empty()) { |
| 78 | + char c = left_stack.top(); left_stack.pop(); |
| 79 | + if (right_stack.empty() || c != key[0] && c != key[1]) { |
| 80 | + right_stack.push(c); |
| 81 | + continue; |
| 82 | + } |
| 83 | + // if we meet the low score pattern |
| 84 | + if ( c == key[1] && right_stack.top() == key[0]){ |
| 85 | + right_stack.pop(); |
| 86 | + score += low; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + right_stack.push(c); |
| 91 | + } |
| 92 | + return score; |
| 93 | + } |
| 94 | +}; |
0 commit comments