My solution in c++


  • 0
    Z

    class Solution {
    public:
    bool wordPattern(string pattern, string str) {
    int* patternCode=new int[pattern.size()];
    for(unsigned int i=0;i<pattern.size();i++)
    patternCode[i]=pattern.find_first_of(pattern[i]);
    vector<string> vec;
    map<string,int> strMap;
    istringstream ss(str);
    string word;
    while(ss>>word)
    vec.push_back(word);
    int* strCode=new int[vec.size()];
    for(unsigned int i=0;i<vec.size();i++)
    {
    if(!strMap.count(vec[i]))
    {
    strMap[vec[i]]=i;
    strCode[i]=i;
    }
    else
    strCode[i]=strMap[vec[i]];

    	}
    	if(pattern.size()!=vec.size())
    		return false;
    	for(unsigned int i=0;i<vec.size();i++)
    		if(patternCode[i]!=strCode[i])
    			return false;
    	return true;
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.