美文网首页工作生活
829. Word Pattern II

829. Word Pattern II

作者: 鸭蛋蛋_8441 | 来源:发表于2019-06-30 08:50 被阅读0次

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

代码:

相关文章

网友评论

    本文标题:829. Word Pattern II

    本文链接:https://www.haomeiwen.com/subject/msuvcctx.html