LeetCode #290 Word Pattern 单词规律

290 Word Pattern 单词规律

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 word in str.

Example :

Example 1:
Input: pattern = "abba", str = "dog cat cat dog"
Output: true

Example 2:
Input:pattern = "abba", str = "dog cat cat fish"
Output: false

Example 3:
Input: pattern = "aaaa", str = "dog cat cat dog"
Output: false

Example 4:
Input: pattern = "abba", str = "dog dog dog dog"
Output: false

Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters that may be separated by a single space.

题目描述:
给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。

这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应规律。

示例 :

示例 1:
输入: pattern = "abba", str = "dog cat cat dog"
输出: true

示例 2:
输入:pattern = "abba", str = "dog cat cat fish"
输出: false

示例 3:
输入: pattern = "aaaa", str = "dog cat cat dog"
输出: false

示例 4:
输入: pattern = "abba", str = "dog dog dog dog"
输出: false

说明:
你可以假设 pattern 只包含小写字母, str 包含了由单个空格分隔的小写字母。

思路:

用 split()函数将字符串按空格分割成字符串数组
采用 map记录单词和 pattern中的字符的对应关系
时间复杂度O(n), 空间复杂度O(n)

代码:
C++:

class Solution 
{
public:
    bool wordPattern(string pattern, string str) 
    {
        const vector& list = split(str, ' '); 
        if (list.size() != pattern.size()) return false;
        map dict1;
        map dict2;
        for (int i = 0; i < list.size(); i++) 
        {
            const string& s = list[i];            
            if (dict1.find(s) != dict1.end()) 
            {
                char tmp = dict1[s];
                if (tmp != pattern[i]) return false;
            } 
            else 
            {
                if (dict2.size() and dict2.find(pattern[i]) != dict2.end()) return false;
                dict1.insert(make_pair(s, pattern[i]));
                dict2.insert(make_pair(pattern[i], s));
            }      
        }
        return true;
    }
private:
    vector split(string& str,const char a) 
    {
        vector strvec;
        string::size_type pos1, pos2;
        pos2 = str.find(a);
        pos1 = 0;
        while (string::npos != pos2) 
        {
            strvec.push_back(str.substr(pos1, pos2 - pos1));
            pos1 = pos2 + 1;
            pos2 = str.find(a, pos1);
        }
        strvec.push_back(str.substr(pos1));
        return strvec;
    }
};

Java:

class Solution {
    public boolean wordPattern(String pattern, String str) {
        char[] chars = pattern.toCharArray();
        String[] strs = str.split(" ");
        if (chars.length != strs.length) return false;
        Map map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i])) {
                if (!map.get(chars[i]).equals(strs[i])) return false;
            } else if (map.containsValue(strs[i])) return false;
            map.put(chars[i], strs[i]);
        }
        return true;
    }
}

Python:

class Solution:
    def wordPattern(self, pattern: str, str: str) -> bool:
        str = str.split(' ')
        if not pattern or not str or len(set(str)) != len(set(pattern)) or len(str) != len(pattern):
            return False
        dict = {}
        for i in range(len(str)):
            if pattern[i] in dict:
                if dict[pattern[i]] == str[i]:
                    continue
                else:
                    return False
            dict[pattern[i]] = str[i]
        return True

你可能感兴趣的:(LeetCode #290 Word Pattern 单词规律)