亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專(zhuān)欄INFORMATION COLUMN

[LeetCode] Group Anagram

kid143 / 1255人閱讀

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;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/77048.html

相關(guān)文章

  • [Leetcode] Valid Anagram 驗(yàn)證變形詞

    摘要:排序法復(fù)雜度時(shí)間空間思路因?yàn)樽冃卧~兩個(gè)單詞對(duì)應(yīng)字母出現(xiàn)的次數(shù)都相同,所以如果將兩個(gè)單詞按字母順序排序,肯定會(huì)變?yōu)橐粋€(gè)字符串,如果字符串不相同,則不是變形詞。 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 評(píng)論0 收藏0
  • Leetcode PHP題解--D85 242. Valid Anagram

    摘要:題目鏈接題目分析判斷給定的兩個(gè)單詞是否同構(gòu)。即,重新排列組合所出現(xiàn)的字母后得到另一個(gè)單詞。思路拆分成數(shù)組后,排序,再拼起來(lái)。最終代碼若覺(jué)得本文章對(duì)你有用,歡迎用愛(ài)發(fā)電資助。 D85 242. Valid Anagram 題目鏈接 242. Valid Anagram 題目分析 判斷給定的兩個(gè)單詞是否同構(gòu)。即,重新排列組合所出現(xiàn)的字母后得到另一個(gè)單詞。 思路 拆分成數(shù)組后,排序,再拼起來(lái)...

    zgbgx 評(píng)論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 評(píng)論0 收藏0
  • [LeetCode] 438. Find All Anagrams in a String [滑動(dòng)窗

    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 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<