【LeetCode】290. Word Pattern

GitHub:https://github.com/BadWaka/leet-code-waka

思路

和 205. Isomorphic Strings 的思路相同

新建四个数组
mapA,mapB用来放置字符串中的元素,
arrA,arrB用来记录字符串在map中对应的位置

两次循环分别遍历两个字符串,并进行放置元素到map数组的操作和在arr中记录位置,完事后判断arrA和arrB是不是相等,如果相等,则返回true

测试示例


【LeetCode】290. Word Pattern_第1张图片
【LeetCode】290. Word Pattern_第2张图片

【LeetCode】290. Word Pattern_第3张图片

代码




    
    290. Word Pattern


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.

你可能感兴趣的:(【LeetCode】290. Word Pattern)