Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

去除字符 #179

Open
TieMuZhen opened this issue Apr 20, 2022 · 0 comments
Open

去除字符 #179

TieMuZhen opened this issue Apr 20, 2022 · 0 comments
Labels

Comments

@TieMuZhen
Copy link
Owner

对输入的字符串,去除其中的字符'b'以及连续出现的'a'和'c'
例如:
'aacbd' -> 'ad'
'aabcd' -> 'ad'
'aaabbccc' -> ''

function _firter(s) {
    let stack = [];
    for (let i = 0; i < s.length; i ++) {
        if(s[i] === 'b') continue;
        if (stack.length) {
            if (s[i] === 'c' && stack[stack.length - 1] === 'a') {
                stack.pop();
                continue;
            }
        }
        stack.push(s[i]);
    }
    return stack.join('');
}
 
let a = 'aaabbccc';
let r = _firter(a);
console.log(r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant