Description
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 substring in str.(i.e if a corresponds to s, then b cannot correspond to s. For example, given pattern = "ab", str = "ss", return false.)
You may assume both pattern and str contains only lowercase letters.
Example
Example 1
Input:
pattern = "abab"
str = "redblueredblue"
Output: true
Explanation: "a"->"red","b"->"blue"
Example 2
Input:
pattern = "aaaa"
str = "asdasdasdasd"
Output: true
Explanation: "a"->"asd"
Example 3
Input:
pattern = "aabb"
str = "xyzabcxzyabc"
Output: false
思路:
这个看答案挺简单,但是想不出来,就是把pattern里的字母一个一个的和string的各种长度的substring来试,如果一个字母在mapping里,就看下它后面的符不符合要求,不在mapping里就挨个儿试一遍,如果到最后没有合适的就是False
代码:
网友评论