您的位置:首页 > 其它

33.Word Pattern

2015-10-20 21:47 260 查看
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.

分析:

1.用map存储字符与字符串间的对应关系

2.如果map中已经存了这个字符,则判断当前value与map中存的oldvalue是否相同,不相同则返回false,相同则继续。

3.如果map中尚未存了这个字符,但是已经有了这个value,则返回false。否则加入<key,value>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: