Group Shifted Strings
Problem Descriptionβ
Visit LeetCode for the full problem description.
Solutionsβ
Solution 1: C# (Best: 228 ms)β
| Metric | Value |
|---|---|
| Runtime | 228 ms |
| Memory | 43 MB |
| Date | 2022-01-14 |
Solution
public class Solution {
public IList<IList<string>> GroupStrings(string[] strings) {
var vals = new Dictionary<string, IList<string>>();
foreach (var s in strings)
{
string key = GetStringKey(s);
if(!vals.ContainsKey(key))
{
vals[key] = new List<string>();
}
vals[key].Add(s);
}
return vals.Values.ToList();
}
private string GetStringKey(string s)
{
string key = "";
for (int i = 1; i < s.Length; i++)
{
int diff = s[i] - s[i-1];
if(diff<0)
{
diff += 26;
}
key += $"{diff},";
}
return key;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Solution | To be analyzed | To be analyzed |