33.Word Pattern

来源:互联网 发布:画线路图用什么软件 编辑:程序博客网 时间:2024/05/18 00:37

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:

  1. pattern = "abba", str = "dog cat cat dog" should return true.
  2. pattern = "abba", str = "dog cat cat fish" should return false.
  3. pattern = "aaaa", str = "dog cat cat dog" should return false.
  4. 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>

0 0
原创粉丝点击