国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Group Anagram

kid143 / 1189人閱讀

Problem

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.

Solution
class Solution {
    public List> groupAnagrams(String[] strs) {
        List> res = new ArrayList<>();
        if (strs == null || strs.length == 0) return res;
        Map> map = new HashMap<>();                        //use HashMap to store grouped anagrams 
        for (String str: strs) {
            char[] charArray = str.toCharArray();
            Arrays.sort(charArray);                                             //sort each string to match existing key
            String anagram = String.valueOf(charArray);
            if (!map.containsKey(anagram)) {                                    //if not existed, create a list
                map.put(anagram, new ArrayList<>());
            }
            map.get(anagram).add(str);                                          //put the original string into its anagram group
        }
        for (Map.Entry> entry: map.entrySet()) {           //use Map.Entry from map.entrySet() to iterate
            List anagrams = entry.getValue();
            Collections.sort(anagrams);                                         //sort it in lexicographic order
            res.add(anagrams);
        }
        return res;
    }
}

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/77048.html

相關文章

  • [Leetcode] Valid Anagram 驗證變形詞

    摘要:排序法復雜度時間空間思路因為變形詞兩個單詞對應字母出現的次數都相同,所以如果將兩個單詞按字母順序排序,肯定會變為一個字符串,如果字符串不相同,則不是變形詞。 Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = a...

    justjavac 評論0 收藏0
  • Leetcode PHP題解--D85 242. Valid Anagram

    摘要:題目鏈接題目分析判斷給定的兩個單詞是否同構。即,重新排列組合所出現的字母后得到另一個單詞。思路拆分成數組后,排序,再拼起來。最終代碼若覺得本文章對你有用,歡迎用愛發電資助。 D85 242. Valid Anagram 題目鏈接 242. Valid Anagram 題目分析 判斷給定的兩個單詞是否同構。即,重新排列組合所出現的字母后得到另一個單詞。 思路 拆分成數組后,排序,再拼起來...

    zgbgx 評論0 收藏0
  • [LeetCode] 760. Find Anagram Mappings

    Problem Given two lists Aand B, and B is an anagram of A. B is an anagram of A means B is made by randomizing the order of the elements in A. We want to find an index mapping P, from A to B. A mapping...

    caozhijian 評論0 收藏0
  • [LeetCode] 438. Find All Anagrams in a String [滑動窗

    Problem Given a string s and a non-empty string p, find all the start indices of ps anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be...

    muzhuyu 評論0 收藏0
  • [LeetCode] 839. Similar String Groups

    Problem Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it equals Y. For example, tars and rats are similar (swapping at positions 0 and 2), and rats ...

    scq000 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<