291 Word Pttern II
Given apattern
and a stringstr
, find ifstr
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter inpattern
and a non-empty substring instr
.
Examples:
pattern =
"abab"
, str ="redblueredblue"
should return true.pattern =
"aaaa"
, str ="asdasdasdasd"
should return true.pattern =
"aabb"
, str ="xyzabcxzyabc"
should return false.
Notes:
You may assume bothpattern
andstr
contains only lowercase letters.
这题跟290的判断条件一样,但因为不能把输入字符串按空格分开,所以要一边递归一边backtrack。时间复杂度好像是O(n^n)
Last updated