Encode and Decode Strings
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 156 ms)β
| Metric | Value |
|---|---|
| Runtime | 156 ms |
| Memory | 46.7 MB |
| Date | 2021-12-18 |
Solution
public class Codec {
// Encodes a list of strings to a single string.
public string encode(IList<string> strs)
{
StringBuilder sb = new StringBuilder();
foreach (var str in strs)
{
sb.Append($"{str.Length}/{str}");
}
return sb.ToString();
}
// Decodes a single string to a list of strings.
public IList<string> decode(string s)
{
List<string> result = new List<string>();
int i = 0;
while (i < s.Length)
{
int slash = s.IndexOf('/', i);
int size = Convert.ToInt32(s.Substring(i, slash - i));
i = slash + size + 1;
result.Add(s.Substring(slash + 1, size));
}
return result;
}
}
// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |