Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
// Group Anagrams - https://leetcode.com/problems/group-anagrams/ class Solution { // Using HashMap to build dictionary // Time Complexity: O(n * klogk) // where k is the length of string // Space Complexity: O(n * k) // Key Clarifications // - Do we need special handling for empty strings // - Can we assume that input strings are all lowercase public List
> groupAnagrams(String[] strs) { Map
> map = new HashMap<>(); for(int i = 0; i < strs.length; i++) { char[] charArray = strs[i].toCharArray(); Arrays.sort(charArray); String sorted = new String(charArray); List
anagrams = map.getOrDefault(sorted, new ArrayList<>()); anagrams.add(strs[i]); map.put(sorted, anagrams); } List
> result = new ArrayList<>(); for(List
anagrams: map.values()) { result.add(anagrams); } return result; } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)