Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = “abba”, str = “dog cat cat dog” should return true.
pattern = “abba”, str = “dog cat cat fish” should return false.
pattern = “aaaa”, str = “dog cat cat dog” should return false.
pattern = “abba”, str = “dog dog dog dog” should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
我开始以为模式都是aabb这种,就是都是a开始这种。不会出现eebb这样的,后来基于这种错误的改造,得到以下方法。

这篇博客讨论了如何解决LeetCode中的290题,即判断一个字符串是否遵循给定的模式。博主最初认为模式只包含连续相同的字母,但后来发现并非如此。他们提出了一个解决方案,通过使用集合在一次遍历中完成匹配检查。给出了几个示例以说明何时返回真和假,并提到了问题的来源和测试用例创建者。
订阅专栏 解锁全文
438

被折叠的 条评论
为什么被折叠?



