从零开始的LC刷题(68): 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 that may be separated by a single space.

刚看完这道题我是懵逼的,想了想我还是懵逼的,写完代码测试时还是懵逼的,这真是简单题吗?先说下思路,为了能让类似abba这种pattern中相同的元素相同并且可以根据目标string更改ab的值,我打算用string指针vector来存储这种关系。首先建立查找用的string指针数组,因为题中说了限小写字母,所以只要26个就够了,可以挨个申请地址空间也可以像我一样再建立26个string的实空间然后依次指向实空间。之后就可以向vector中push了,比如abba我就依次push:patt[a-97], patt[b-97]...。之后就是读入目标串,在处理第n个单词时,如果vector第n个元素是空串就赋值为当前单词并n++,如果不为空则与当前单词比较,相同则n++,不同则返回false。在调试时发现如果单词长于pattern会溢出,于是每次循环都要比较一下n和vector的size,后来发现如果pattern多于单词数会出现假true的问题,所以循环后又加了一个n和vector的size的比较。最后一个发现的问题就是如果abba中a和b是相同的单词,比如“dog dog dog dog”也会出现假true的现象,所以最后用hash map比较了一下26个真string的值,如果有相同的会返回false。

比较啰嗦但是速度很好,不过存储因为string数组的关系还可以在降低:

Success

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Word Pattern.

Memory Usage: 8.5 MB, less than 84.50% of C++ online submissions for Word Pattern.

代码:

class Solution {
public:
    bool wordPattern(string pattern, string str) {
        vector<string*> lib;
        string* patt[26]={nullptr};
        string realpatt[26]={""};
        for(int i=0;i<26;i++){
            patt[i]=&realpatt[i];
        }
        for(int i=0;i<pattern.size();i++){
            lib.push_back(patt[pattern[i]-97]);
        }
        int n=0;
        for(int i=0;i<str.size();i++){
            if(n>=lib.size()){return false;}
            string s="";
            while(str[i]!=' '&&i<str.size()){
                s+=str[i];
                i++;
            }
            if(*lib[n]==""){*lib[n]=s;n++;continue;}
            else if(*lib[n]==s){n++;continue;}
            else {return false;}
        }
        if(n!=lib.size()){return false;}
        map<string,int> lastcheck;
        for(int i=0;i<26;i++){
            if(realpatt[i]==""){continue;}
            else{
                if(lastcheck.count(realpatt[i])){return false;}
                else{lastcheck.insert(map<string, int>::value_type (realpatt[i], i));}
            }
        }
        return true;
    }
};

之后看了看submission中的一些结果,感觉双map也能解决这个问题而且代码更简洁,但是memory似乎会占用更多。这里贴一下结果和代码:

Success

Runtime: 4 ms, faster than 78.40% of C++ online submissions for Word Pattern.

Memory Usage: 8.9 MB, less than 10.62% of C++ online submissions for Word Pattern.

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

};

代码原地址:

https://leetcode.com/problems/word-pattern/discuss/73409/Short-C%2B%2B-read-words-on-the-fly

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值