classSolution{public:vector<string>generateAbbreviations(stringword){vector<string>ans;dfs(word,0,0,{},ans);returnans;}private:voiddfs(conststring&word,inti,intcount,vector<string>&&path,vector<string>&ans){if(i==word.length()){ans.push_back(join(path)+getCountString(count));return;}// Abbreviate word[i]dfs(word,i+1,count+1,move(path),ans);// Keep word[i], so consume the count as a stringpath.push_back(getCountString(count)+word[i]);dfs(word,i+1,0,move(path),ans);// Reset count to 0path.pop_back();}stringgetCountString(intcount){returncount>0?to_string(count):"";}stringjoin(constvector<string>&path){stringjoined;for(conststring&s:path)joined+=s;returnjoined;};};
classSolution{publicList<String>generateAbbreviations(Stringword){List<String>ans=newArrayList<>();dfs(word,0,0,newStringBuilder(),ans);returnans;}privatevoiddfs(finalStringword,inti,intcount,StringBuildersb,List<String>ans){if(i==word.length()){finalintlength=sb.length();ans.add(sb.append(getCountString(count)).toString());sb.setLength(length);return;}// Abbreviate word.charAt(i)dfs(word,i+1,count+1,sb,ans);// Keep word.charAt(i), so consume the count as a stringfinalintlength=sb.length();// Reset count to 0dfs(word,i+1,0,sb.append(getCountString(count)).append(word.charAt(i)),ans);sb.setLength(length);}privateStringgetCountString(intcount){returncount>0?String.valueOf(count):"";}}
classSolution:defgenerateAbbreviations(self,word:str)->List[str]:ans=[]defgetCountString(count:int)->str:returnstr(count)ifcount>0else''defdfs(i:int,count:int,path:List[str])->None:ifi==len(word):ans.append(''.join(path)+getCountString(count))return# Abbreviate word[i]dfs(i+1,count+1,path)# Keep word[i], so consume the count as a stringpath.append(getCountString(count)+word[i])dfs(i+1,0,path)# Reset count to 0path.pop()dfs(0,0,[])returnans