Given a pattern
and a string str
, find if str
follows the same pattern.
Examples:
"abba"
, str = "dog cat cat dog"
should return true."abba"
, str = "dog cat cat fish"
should return false."aaaa"
, str = "dog cat cat dog"
should return false."abba"
, str = "dog dog dog dog"
should return false.
Notes:
pattern
and str
contains only lowercase alphabetical letters.pattern
and str
do not have leading or trailing spaces.str
is separated by a single space.pattern
must map to a word with length that is at least 1.Credits:
Special thanks to @minglotus6 for adding this problem and creating all test cases.
原文:http://www.cnblogs.com/grandyang/p/4857022.html