290. Word Pattern

本文深入探讨了模式匹配算法,包括C++和Java中使用映射(map)和istringstream进行字符串模式匹配的方法,以及Python中简洁的实现方式。通过多个示例,如abba模式与dogcatcatdog字符串的匹配,展示了不同编程语言下模式匹配的实现细节和效率对比。

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 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 separated by a single space.

 

Approach #1: using map with C++.[Wrong Answer.

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        vector<string> strArr;
        helper(str, strArr);
        if (pattern.length() != strArr.size()) return false;
        map<char, string> mp;
        for (int i = 0; i < pattern.length(); ++i) {
            if (mp[pattern[i]] == "") mp[pattern[i]] = strArr[i];
            else if (mp[pattern[i]] != strArr[i]) return false;
        }
        return true;
    }
    
    void helper(string str, vector<string>& strArr) {
        int len = str.length();
        string temp = "";
        for (int i = 0; i <= len; ++i) {
            temp += str[i];
            if (str[i] == ' ' || str[i] == '\0') {
                strArr.push_back(temp);
                temp = "";
            }
        }
    }
};

 

Approach #2: Using istringstream with C++.

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        map<char, int> mapChar;
        map<string, int> mapString;
        istringstream in(str);
        int i = 0, n = pattern.size();
        for (string word; in >> word; ++i) {
            if (i == n || mapChar[pattern[i]] != mapString[word])
                return false;
            mapChar[pattern[i]] = mapString[word] = i + 1;
        }
        return i == n;
    }
};

  

Approach #2: Java.

class Solution {
    public boolean wordPattern(String pattern, String str) {
        String[] words = str.split(" ");
        if (words.length != pattern.length()) return false;
        Map index = new HashMap();
        for (Integer i = 0; i < words.length; ++i) {
            if (index.put(pattern.charAt(i), i) != index.put(words[i], i)) return false;
        }
        return true;
    }
}

  

Approach: #3: Python.

class Solution(object):
    def wordPattern(self, pattern, str):
        """
        :type pattern: str
        :type str: str
        :rtype: bool
        """
        s = pattern
        t = str.split()
        return map(s.find, s) == map(t.index, t)

  

Time SubmittedStatusRuntimeLanguage
a few seconds agoAccepted1 msjava
4 minutes agoAccepted20 mspython
6 minutes agoAccepted0 mscpp

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/9958735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值