You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Attempt 1: Use a special pattern that stores the length of the string and the original string
publicclassCodec {
// Encodes a list of strings to a single string.publicStringencode(List<String> strs) {
StringBuilderstringBuilder = newStringBuilder();
for (Stringstr : strs) {
stringBuilder.append(str.length());
stringBuilder.append(' ');
stringBuilder.append(str);
}
returnstringBuilder.toString();
}
// Decodes a single string to a list of strings.publicList<String> decode(Strings) {
intlen = s.length();
List<String> ans = newArrayList<>();
inti = 0;
while (i < len) {
intwordLen = 0;
charch = s.charAt(i);
while (ch != ' ') {
wordLen = wordLen * 10 + ch - '0';
i++;
ch = s.charAt(i);
}
i++;
ans.add(s.substring(i, i + wordLen));
i = i + wordLen;
}
returnans;
}
}
// Your Codec object will be instantiated and called as such:// Codec codec = new Codec();// codec.decode(codec.encode(strs));