classCodec:defencode(self,strs:List[str])->str:"""Encodes a list of strings to a single string."""ans=[]forsinstrs:ans.append('{:4}'.format(len(s))+s)return''.join(ans)defdecode(self,s:str)->List[str]:"""Decodes a single string to a list of strings."""ans=[]i,n=0,len(s)whilei<n:size=int(s[i:i+4])i+=4ans.append(s[i:i+size])i+=sizereturnans# Your Codec object will be instantiated and called as such:# codec = Codec()# codec.decode(codec.encode(strs))
publicclassCodec{// Encodes a list of strings to a single string.publicStringencode(List<String>strs){StringBuilderans=newStringBuilder();for(Strings:strs){ans.append((char)s.length()).append(s);}returnans.toString();}// Decodes a single string to a list of strings.publicList<String>decode(Strings){List<String>ans=newArrayList<>();inti=0,n=s.length();while(i<n){intsize=s.charAt(i++);ans.add(s.substring(i,i+size));i+=size;}returnans;}}// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(strs));
classCodec{public:// Encodes a list of strings to a single string.stringencode(vector<string>&strs){stringans;for(strings:strs){intsize=s.size();ans+=string((constchar*)&size,sizeof(size));ans+=s;}returnans;}// Decodes a single string to a list of strings.vector<string>decode(strings){vector<string>ans;inti=0,n=s.size();intsize=0;while(i<n){memcpy(&size,s.data()+i,sizeof(size));i+=sizeof(size);ans.push_back(s.substr(i,size));i+=size;}returnans;}};// Your Codec object will be instantiated and called as such:// Codec codec;// codec.decode(codec.encode(strs));
typeCodecstruct{}// Encodes a list of strings to a single string.func(codec*Codec)Encode(strs[]string)string{ans:=&bytes.Buffer{}for_,s:=rangestrs{t:=fmt.Sprintf("%04d",len(s))ans.WriteString(t)ans.WriteString(s)}returnans.String()}// Decodes a single string to a list of strings.func(codec*Codec)Decode(strsstring)[]string{ans:=[]string{}i,n:=0,len(strs)fori<n{t:=strs[i:i+4]i+=4size,_:=strconv.Atoi(t)ans=append(ans,strs[i:i+size])i+=size}returnans}// Your Codec object will be instantiated and called as such:// var codec Codec// codec.Decode(codec.Encode(strs));