Meta VO 面试真题解析:删除相邻连续字母直至无法继续|coding interview

17次阅读
没有评论

Given a string containing uppercase characters A-Z, remove pairs of adjacent characters if they are consecutive characters in the alphabet (for example, "AB" or "BA") until no more characters can be removed.

Return the resultant string.

这道题要求你不断删除字符串中相邻且字母序连续的一对字符,例如“AB”或“BA”,直到字符串中再也不存在这样的相邻对为止。常见做法是使用栈从左到右扫描:每读入一个字符,就与栈顶比较,如果两者在字母表中相差 1,则说明可以相互抵消并弹栈;否则将当前字符入栈。这样可以在线性时间内完成所有连锁删除,最终栈中剩下的内容就是答案。

正文完
 0