
g0001_0100.s0049_group_anagrams.Solution.cs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
namespace LeetCodeNet.G0001_0100.S0049_group_anagrams {
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #String #Hash_Table #Sorting
// #Data_Structure_II_Day_8_String #Programming_Skills_II_Day_11 #Udemy_Strings
// #Big_O_Time_O(n*k_log_k)_Space_O(n) #2024_01_04_Time_145_ms_(96.20%)_Space_80.2_MB_(38.80%)
public class Solution {
public IList> GroupAnagrams(string[] strs) {
var map = new Dictionary>();
// allocate memory only once and reuse it to sort the chars of each s in strs.
var buffer = new char[10000];
var bufferSpan = new Span(buffer);
foreach (var s in strs) {
s.CopyTo(bufferSpan);
Array.Sort(buffer, 0, s.Length);
var key = new string(buffer, 0, s.Length);
if (!map.TryGetValue(key, out var value)) {
map[key] = value = new List();
}
value.Add(s);
}
return map.Values.Cast>().ToList();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy