classCodec{public:// Encodes a list of strings to a single string.stringencode(vector<string>&strs){stringencoded;for(conststring&s:strs)encoded+=to_string(s.length())+'/'+s;returnencoded;}// Decodes a single string to a list of strings.vector<string>decode(strings){vector<string>decoded;for(inti=0;i<s.length();){constintslash=s.find('/',i);constintlength=stoi(s.substr(i,slash-i));i=slash+length+1;decoded.push_back(s.substr(slash+1,i-slash-1));}returndecoded;}};
publicclassCodec{// Encodes a list of strings to a single string.publicStringencode(List<String>strs){StringBuilderencoded=newStringBuilder();for(finalStrings:strs)encoded.append(s.length()).append('/').append(s);returnencoded.toString();}// Decodes a single string to a list of strings.publicList<String>decode(Strings){List<String>decoded=newArrayList<>();for(inti=0;i<s.length();){finalintslash=s.indexOf('/',i);finalintlength=Integer.parseInt(s.substring(i,slash));i=slash+length+1;decoded.add(s.substring(slash+1,i));}returndecoded;}}
classCodec:defencode(self,strs:List[str])->str:"""Encodes a list of strings to a single string."""return''.join(str(len(s))+'/'+sforsinstrs)defdecode(self,s:str)->List[str]:"""Decodes a single to a list of strings."""decoded=[]i=0whilei<len(s):slash=s.find('/',i)length=int(s[i:slash])i=slash+length+1decoded.append(s[slash+1:i])returndecoded