49. Group Anagrams

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]

Note:

All inputs will be in lowercase.
The order of your output does not matter.

class Solution {    
    List<List<String>> res = new ArrayList<>();
    Map<String, Integer> hmp = new HashMap<>();
    public List<List<String>> groupAnagrams(String[] strs) {
        int Len=0;
        for(int i=0; i<strs.length; i++){
            findPlace(strs[i]);
        }
        return res;
    }
    
    public void findPlace(String str) {
        char[] strC = str.toCharArray();
        Arrays.sort(strC);
        String StrC = new String(strC);
        int index;
        if (hmp.containsKey(StrC)) {
            index=hmp.get(StrC);
            res.get(index).add(str);
        } else {
            List<String> sub = new ArrayList<>();
            sub.add(str);
            res.add(sub);
            hmp.put(StrC, res.size()-1);
        }
    }
}
/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function(strs) {
    const len = strs.length
    const res = []
    const map = new Map()
    for (let i = 0; i < len; i++) {
        const str = strs[i].split('').sort().join('')
        if (!map.has(str)) {
            const arr = []
            arr.push(strs[i])
            map.set(str, arr)
        } else {
            const arr = map.get(str)
            arr.push(strs[i])
            map.set(str, arr)
        }
    }
    map.forEach(value => {
        res.push(value)
    })
    return res
};
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        HashMap<String, List<String>> resultMap = new HashMap<>();
        List<List<String>> resu = new ArrayList<>();
        String[] notSortedCopyOfArr = Arrays.copyOf(strs, strs.length);

        for(int i = 0; i< strs.length; i++){
            strs[i] = sortString(strs[i]);
        }
        for(int i = 0; i < strs.length; i++){
            List<String> itemsList = resultMap.get(strs[i]);
            if(itemsList == null) {
                //create new list in case if no other anagrams added 
                itemsList = new ArrayList<String>();
                itemsList.add(notSortedCopyOfArr[i]);
                resultMap.put(strs[i], itemsList);
            } else {
                // add if item is not already in list
                itemsList.add(notSortedCopyOfArr[i]);
            }
        }
        for(List<String> value : resultMap.values()){
            resu.add(value);
        }
        return resu;
    }
    public static String sortString(String inputString) { 
        char tempArray[] = inputString.toCharArray(); 
        Arrays.sort(tempArray); 
        return new String(tempArray); 
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值