zoukankan      html  css  js  c++  java
  • 290. Word Pattern

    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] == '') {
                    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 ago Accepted 1 ms java
    4 minutes ago Accepted 20 ms python
    6 minutes ago Accepted 0 ms cpp
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    【转】sublime text 2中Emmet插件8个常用的技巧
    程序猿崛起3——这一次,我用行动说话
    《Effective Java》学习笔记——积累和激励
    程序猿崛起2——互联网时代下的新潮流和新活法
    【非技术】做好属于自己的作品,然后让世界所有人都记住你
    【非技术】实现理想的第一步就是做自己
    【原创】程序猿崛起
    人生苦短,我用python——当我在玩python的时候我玩些什么
    一个新人如何学习在大型系统中添加新功能和Debug
    一个应届毕业生入职30天后的工作总结——作息
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9958735.html
Copyright © 2011-2022 走看看