classSolution{public:stringminAbbreviation(stringtarget,vector<string>&dictionary){constintm=target.length();vector<int>masks;for(conststring&word:dictionary){if(word.length()!=m)continue;masks.push_back(getMask(target,word));}if(masks.empty())returnto_string(m);vector<string>abbrs;constintmaxCand=pow(2,m);// For all candidate representation of targetfor(intcand=0;cand<maxCand;++cand)// All masks have at lease one bit different from candidateif(all_of(begin(masks),end(masks),[cand](intmask){returncand&mask;}))abbrs.push_back(getAbbr(target,cand));stringans=target;for(conststring&abbr:abbrs)if(getAbbrLen(abbr)<getAbbrLen(ans))ans=abbr;returnans;}private:intgetMask(conststring&target,conststring&word){constintm=target.length();// mask[i] = 0 := target[i] == word[i]// mask[i] = 1 := target[i] != word[i]// E.g. target = "apple"// word = "blade"// mask = 11110intmask=0;for(inti=0;i<m;++i)if(word[i]!=target[i])mask|=1<<m-1-i;returnmask;}stringgetAbbr(conststring&target,intcand){constintm=target.length();stringabbr;intreplacedCount=0;for(inti=0;i<m;++i)if(cand>>m-1-i&1){// cand[i] = 1, abbr should show the original characterif(replacedCount>0)abbr+=to_string(replacedCount);abbr+=target[i];replacedCount=0;}else{// cand[i] = 0, abbr can be replaced++replacedCount;}if(replacedCount>0)abbr+=to_string(replacedCount);returnabbr;}intgetAbbrLen(conststring&abbr){intabbrLen=0;inti=0;intj=0;while(i<abbr.length()){if(isalpha(abbr[j]))++j;elsewhile(j<abbr.length()&&isdigit(abbr[j]))++j;++abbrLen;i=j;}returnabbrLen;}};
classSolution{publicStringminAbbreviation(Stringtarget,String[]dictionary){finalintm=target.length();List<Integer>masks=newArrayList<>();for(finalStringword:dictionary){if(word.length()!=m)continue;masks.add(getMask(target,word));}if(masks.isEmpty())returnString.valueOf(m);List<String>abbrs=newArrayList<>();finalintmaxCand=(int)Math.pow(2,m);// For all candidate representation of targetfor(inti=0;i<maxCand;++i){finalintcand=i;// All masks have at lease one bit different from candidateif(masks.stream().allMatch(mask->(cand&mask)>0))abbrs.add(getAbbr(target,cand));}Stringans=target;for(finalStringabbr:abbrs)if(getAbbrLen(abbr)<getAbbrLen(ans))ans=abbr;returnans;}privateintgetMask(finalStringtarget,finalStringword){finalintm=target.length();// mask[i] = 0 := target[i] == word[i]// mask[i] = 1 := target[i] != word[i]// E.g. target = "apple"// word = "blade"// mask = 11110intmask=0;for(inti=0;i<m;++i)if(word.charAt(i)!=target.charAt(i))mask|=1<<m-1-i;returnmask;}StringgetAbbr(finalStringtarget,intcand){finalintm=target.length();StringBuildersb=newStringBuilder();intreplacedCount=0;for(inti=0;i<m;++i)if((cand>>m-1-i&1)==1){// cand[i] = 1, abbr should show the original characterif(replacedCount>0)sb.append(replacedCount);sb.append(target.charAt(i));replacedCount=0;}else{// cand[i] = 0, abbr can be replaced++replacedCount;}if(replacedCount>0)sb.append(replacedCount);returnsb.toString();}intgetAbbrLen(finalStringabbr){intabbrLen=0;inti=0;intj=0;while(i<abbr.length()){if(Character.isAlphabetic(abbr.charAt(j)))++j;elsewhile(j<abbr.length()&&Character.isDigit(abbr.charAt(j)))++j;++abbrLen;i=j;}returnabbrLen;}}
classSolution:defminAbbreviation(self,target:str,dictionary:List[str])->str:m=len(target)defgetMask(word:str)->int:# mask[i] = 0 := target[i] == word[i]# mask[i] = 1 := target[i] != word[i]# E.g. target = "apple"# word = "blade"# mask = 11110mask=0fori,cinenumerate(word):ifc!=target[i]:mask|=1<<m-1-ireturnmaskmasks=[getMask(word)forwordindictionaryiflen(word)==m]ifnotmasks:returnstr(m)abbrs=[]defgetAbbr(cand:int)->str:abbr=''replacedCount=0fori,cinenumerate(target):ifcand>>m-1-i&1:# cand[i] = 1, abbr should show the original characterifreplacedCount:abbr+=str(replacedCount)abbr+=creplacedCount=0else:# cand[i] = 0, abbr can be replacedreplacedCount+=1ifreplacedCount:abbr+=str(replacedCount)returnabbr# For all candidate representation of targetforcandinrange(2**m):# All masks have at lease one bit different from candidateifall(cand&maskformaskinmasks):abbr=getAbbr(cand)abbrs.append(abbr)defgetAbbrLen(abbr:str)->int:abbrLen=0i=0j=0whilei<len(abbr):ifabbr[j].isalpha():j+=1else:whilej<len(abbr)andabbr[j].isdigit():j+=1abbrLen+=1i=jreturnabbrLenreturnmin(abbrs,key=lambdax:getAbbrLen(x))