Skip to main content

Group Shifted Strings

LeetCode Link

Problem Description​

Visit LeetCode for the full problem description.


Solutions​

Solution 1: C# (Best: 228 ms)​

MetricValue
Runtime228 ms
Memory43 MB
Date2022-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​

ApproachTimeSpace
SolutionTo be analyzedTo be analyzed