Hope I have got the problem right.
Here is the C++ solution
#include <bits/stdc++.h>
usingnamespacestd;classObjectSpecial{map<string,int>mapSpecial;public:// constructorObjectSpecial(map<string,int>mapObject){this->mapSpecial=mapObject;}// cpp map already stores the keys in alphabetical order// so start from first and if we find a key that starts with the partial key// then return its value// if key not found then return 0intoperator[](strings){for(autoit:mapSpecial){// check if key starts with the given partial key s// if true then return corresponding valueif(it.first.rfind(s,0)==0)returnit.second;}return0;}};ObjectSpecialpartialKeys(map<string,int>m){returnObjectSpecial(m);}// main functionintmain(){ObjectSpecialo=partialKeys({{"abcd",1},{"abbd",2}});cout<<o["abcd"]<<"\n";// output : 1cout<<o["ab"]<<"\n";// output : 2cout<<o["k"]<<"\n";// output : 0 as key is not presentcout<<o["abc"]<<"\n";// output : 1cout<<o["abd"]<<"\n";// output : 0 as key is not presentreturn0;}
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hope I have got the problem right.
Here is the C++ solution