LeetCode:判断str字符串是否遵循模板字符串

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 a letter in pattern and a 
non-tmpty word in str.

Examples:

pattern = "abba", str = "dog cat cat dog" should return true.
pattern = "abba", str = "dog cat cat fish" should return false.
pattern = "aaaa", str = "dog cat cat dog" should return false.
pattern = "abba", str = "dog dog dog dog" should return false.

Note:
You may assume pattern contains only lowercase letters ,and str contains lowercase letters 
sepatated by a single space.

题目大意:
给定一个模板字符串和一个str字符串,判断str字符串是否遵循模板字符串?

解题思路:
方法1:
利用hashmap进行存储,key存储单个字符,value存储str其中一个.注意遍历过程中需要剔除空格.可以使用istringstream
分割字符串.注意需要判断key值是否存在,存在则需要判断value值是否相等;若没有还需判断是否已经存在value.

方法2
算法优化,可以直接用set容器或者map容器存储pattern的单个字符和strs数组中的对应的位置的字符串,在insert过程
中返回pair<iterator,bool>,可以根据interator判断value是否相等

方法3
不用map的insert,
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <set>

using namespace std;

class Solution{
public:
    bool wordPattern(string pattern,string str){
        //可以直接枚举str字符串剔除空格
        istringstream is(str);//剔除空格
        vector<string> strs;
        string s;
        while(is>>s){
            strs.push_back(s);
        }
        if(strs.size()!=pattern.size()) return false;//不相等直接返回
        map<char,string> ans;
        set<string> temp;
        int i=0;
        for(auto& j:strs){
            if(ans.count(pattern[i])){
                if(ans[pattern[i]]!=j)//字符已经存在,字符串不等
                   return false;
            }else{
                if(temp.count(j))//字符不同在插入之前需要判断字符串是否已经存在
                   return false;
                temp.insert(j);
                ans.insert({pattern[i],j});//make_pair(pattern[i],j)
            }
            i++;
        }
        return true;
    }
    bool wordPatternOne(string pattern,string str){
        vector<string> strs;
        string tmp;
        tmp.clear();
        int pos=0;
        for(int i=0;i<str.size();++i){
            if(str[i]==' '){
                strs.push_back(str.substr(i-pos,pos));//pos是substring的长度
                pos=0;
            }else{
               pos++;  
            }         
        }
        strs.push_back(str.substr(str.size()-pos,pos));
        
        if(pattern.size()!=strs.size()) 
            return false;

        map<string,int> ans;
        map<char,int> res;
        for(int i=0;i<strs.size();++i){ 
            if(res.insert({pattern[i],i}).first->second!=ans.insert({strs[i],i}).first->second){
                return false;
            }
        }
        return true;
    }
    bool wordPatternTwo(string pattern,string str){
        stringstream ss(str);
        map<string,char> ans;
        map<char,string> res;
        string s;
        for(auto& c:pattern){
            if(!(ss>>s)||(res.count(c)==1&&res[c]!=s)||(ans.count(s)==1&&ans[s]!=c)){
                return false;
            }
            ans[s]=c;
            res[c]=s;
        }
        return (ss>>s)?false:true;
    }
};

int main(int argc,char* argv[]){
    string pattern="abc", str = "b c a";
    //cout<<Solution().wordPattern(pattern,str)<<endl;
    cout<<Solution().wordPatternTwo(pattern,str)<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路上的追梦人

您的鼓励就是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值