Skip to content

Latest commit

 

History

History

minimum-string-length-after-removing-substrings

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Minimum string length after removing substrings

Problem link

Solutions

Solution.cpp

// https://leetcode.com/problems/minimum-string-length-after-removing-substrings/

class Solution {
 public:
  int minLength(string s) {
    vector<char> stack;
    for (char c : s) {
      if (!stack.empty() and ((c == 'D' and stack.back() == 'C') or
                              (c == 'B' and stack.back() == 'A')))
        stack.pop_back();
      else
        stack.push_back(c);
    }
    return stack.size();
  }
};

Tags