Google VO 面试真题解析:Buddy Strings 分组与带括号字母公式化简

19次阅读
没有评论

Given a list/array of strings, find and group together all strings that are "buddies" with one another. You may use any data structure of your choosing to return the grouping of buddies.

Two strings are "buddies" if they are the same length, and the characters are equal distance from each other.

For example, "aaa" and "zzz" are buddies, but "aaa" would not be buddies with "abc".

Given a formula of letters with parentheses, return a simplified equivalent version of the equation without parentheses.

Examples:

a-(b+c) -> a-b-c
a-(a-b) -> b

这道题包含两个经典面试场景:第一个是对字符串按“buddy”关系分组,核心在于先准确判断两串是否满足同长度以及字符位置对应的距离关系,再用哈希、并查集或分桶等结构把它们归类;第二个是带括号的字母表达式化简,重点在于处理括号前的负号、维护符号状态,并将括号内字符展开为等价的无括号形式。做题时要特别注意示例中的等价转换规则,避免把字符顺序或符号传播处理错。

正文完
 0