Pergunta de entrevista da empresa Vedantu

Given an array of strings, group anagrams together. Example: Input: ["eat", "tea", "tan", "ate", "nat", "bat"], Output: [ ["ate","eat","tea"], ["nat","tan"], ["bat"] ]

Resposta da entrevista

Sigiloso

4 de ago. de 2020

class Solution { public List> groupAnagrams(String[] words) { List> result = new ArrayList(); HashMap> seen = new HashMap(); for(String word : words){ char[] map = new char[26]; for(char x : word.toCharArray()){ map[x - 'a']++; } String str = ""; for(int x : map){ str += x; } if(seen.containsKey(str)){ seen.get(str).add(word); } else{ ArrayList temp = new ArrayList(); temp.add(word); result.add(temp); seen.put(str, temp); } } return result; } }

2