#ifndef wordpattern_hpp#defineWordpattern_hpp#include<iostream>#include<map>#include<vector>#include<sstream>#include<stdio.h>using namespacestd;classSolution { Public: BOOLWordpattern (stringPatternstringstr) {Map<Char,string>MCs; Map<string,Char>MSC; StringStream SS{STR}; stringitem; for(Auto C:pattern) {if(ss>>Item) { if(Mcs.end ()! =Mcs.find (c)) { if(Mcs[c]! =Item) { return false; } }Else{ if(Msc.end ()! =Msc.find (item)) { return false; } Mcs[c]=item; Msc[item]=C; } }Else{ return false; } } if(ss>>Item)return false; return true; } voidTest () {structwps{stringPat; stringstr; BOOLRes; }; Solution S; Vector<wps> tests{wps{"AAA","Cat Cat Cat",true}, wps{"Abba","dog cat Cat Dog",true}, wps{"Abba","dog cat Cat Fish",false}, wps{"AAAA","dog cat Cat Dog",false}, wps{"Abba","dog Dog Dog Dog",false}, wps{"AAA"," aa aa, AA AA",false}}; for(Auto t:tests) {cout<<"Pattern:"<< T.pat <<", str: \ ""<< T.str <<"\" "; cout<< (T.res = = S.wordpattern (T.pat, t.str))?"Pass":"fail") <<'\ n'; } }};#endif/* WORDPATTERN_HPP */
The program uses the STL map and vector to be concise.
#290 Wordpattern