1717. Maximum Score From Removing Substrings

 JAVA

class Solution {
    int points = 0;

    public int maximumGain(String s, int x, int y) {
        if (x >= y) {
            s = remove1(s, x);
            s = remove2(s, y);
        } else {
            s = remove2(s, y);
            s = remove1(s, x);
        }
        return points;
    }

    public String remove1(String s, int x) {
        StringBuffer sb = new StringBuffer();
        int length = s.length();
        int index = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (index > 0 && c == 'b' && sb.charAt(index - 1) == 'a') {
                points += x;
                sb.deleteCharAt(index - 1);
                index--;
            } else {
                sb.append(c);
                index++;
            }
        }
        return sb.toString();
    }

    public String remove2(String s, int y) {
        StringBuffer sb = new StringBuffer();
        int length = s.length();
        int index = 0;
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (index > 0 && c == 'a' && sb.charAt(index - 1) == 'b') {
                points += y;
                sb.deleteCharAt(index - 1);
                index--;
            } else {
                sb.append(c);
                index++;
            }
        }
        return sb.toString();
    }
}

class Solution {
    public int maximumGain(String s, int x, int y) {
        if (x < y) {
            return maximumGain(new StringBuilder(s).reverse().toString(), y, x);
        }
        int ans = 0;
        Deque<Character> stk1 = new ArrayDeque<>();
        Deque<Character> stk2 = new ArrayDeque<>();
        for (char c : s.toCharArray()) {
            if (c != 'b') {
                stk1.push(c);
            } else {
                if (!stk1.isEmpty() && stk1.peek() == 'a') {
                    stk1.pop();
                    ans += x;
                } else {
                    stk1.push(c);
                }
            }
        }
        while (!stk1.isEmpty()) {
            char c = stk1.pop();
            if (c != 'b') {
                stk2.push(c);
            } else {
                if (!stk2.isEmpty() && stk2.peek() == 'a') {
                    stk2.pop();
                    ans += y;
                } else {
                    stk2.push(c);
                }
            }
        }
        return ans;
    }
}

C++

  • class Solution {
    public:
        int maximumGain(string s, int x, int y) {
            if (x < y) {
                reverse(s.begin(), s.end());
                return maximumGain(s, y, x);
            }
            int ans = 0;
            stack<char> stk1;
            stack<char> stk2;
            for (char c : s) {
                if (c != 'b')
                    stk1.push(c);
                else {
                    if (!stk1.empty() && stk1.top() == 'a') {
                        stk1.pop();
                        ans += x;
                    } else
                        stk1.push(c);
                }
            }
            while (!stk1.empty()) {
                char c = stk1.top();
                stk1.pop();
                if (c != 'b')
                    stk2.push(c);
                else {
                    if (!stk2.empty() && stk2.top() == 'a') {
                        stk2.pop();
                        ans += y;
                    } else
                        stk2.push(c);
                }
            }
            return ans;
        }
    };

Comments