Code Capriccio - Stack and Queue - Delete all adjacent duplicates in a string

Remove all adjacent duplicates in a string

This section corresponds to Code Capriccio: Code Capriccio , and the corresponding video link is: None yet

exercise

Question link: 1047. Delete all adjacent duplicates in a string - LeetCode

Given a string S consisting of lowercase letters, the duplicate removal operation selects two adjacent and identical letters and deletes them.

Repeat the duplicate deletion operation on S until deletion cannot continue.

Returns the final string after all duplicate removal operations have been completed. The answer is guaranteed to be unique.

输入:"abbaca"
输出:"ca"
解释:
例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。

stack

Adjacent duplicates are deleted, and if they are still adjacent after deletion, they are deleted again. In this way, it cannot be simply used s[i]==s[i-1]to judge. The traversal is still s[i], but the element to be compared is not the string to the left of s[i]-1at this time s[i], so it can be implemented using the stack. s[i]The string on the left is the top of the stack. If it does not meet the conditions, it will be pushed onto the stack. If it meets the conditions, it will be popped out of the stack and deleted.

The code is as follows. The logic is relatively simple. It should be noted that there is no need to use the stack to save the results first and then reverse them like the code capriccio. As long as the string is traversed from back to front, the results obtained will be in the correct order.

class Solution {
    
    
   public:
    string removeDuplicates(string s) {
    
    
        stack<int> myStack;
        // 逆序遍历得到的即为正确顺序
        for (int i = s.size()-1; i >= 0; i--) {
    
    
            if (!myStack.empty() && s[i] == myStack.top()) {
    
    
                myStack.pop();
            } else {
    
    
                myStack.push(s[i]);
            }
        }
        string res;
        while (!myStack.empty()) {
    
    
            res += myStack.top();
            myStack.pop();
        }
        return res;
    }
};
  • Time complexity: O( nnn ), where n is the length of string s. Just iterate through the input string once, pushing characters onto the stack and popping them one by one.
  • Space complexity: O( nnn ). In the worst case, the entire string may be stored on the stack (e.g. the string has no repeating characters), so it requires O(n) extra space. The output string also requires O(n) space. Therefore, the total space complexity is O(n).

string implementation

C++'s string has interfaces similar to popping and pushing into the stack, so you can directly use string to solve problems. This eliminates the need to first save the result on the stack and then convert the result into a string.

class Solution {
    
    
   public:
    string removeDuplicates(string s) {
    
    
        string res;
        for (int i = 0; i < s.size(); i++) {
    
    
            if (!res.empty() && s[i] == res[res.size()-1]) {
    
    
                res.pop_back();
            } else {
    
    
                res.push_back(s[i]);
            }
        }
        return res;
    }
};
  • Time complexity: O( nnn ), where n is the length of string s. Because the for loop traverses the input string once, the time complexity is O(n).
  • Space complexity: O( nnn ). In the worst case, for example, when all characters in the input string are different, the output string also requires O(n) extra space. Therefore, the total space complexity is O(n).

おすすめ

転載: blog.csdn.net/zss192/article/details/130575728