1. 程式人生 > >LeetCode OJ 之 Anagrams (迴文構詞法)

LeetCode OJ 之 Anagrams (迴文構詞法)

題目:

Given an array of strings, return all groups of strings that are anagrams.(給定一組字串,返回符合迴文構詞法的單片語,即如果某幾個單詞是迴文單詞,則放入一個容器中並返回)。

Note: All inputs will be in lower-case.(所有的輸入都是小寫)

Anagram(迴文構詞法)是指打亂字母順序從而得到新的單詞,比如 "dormitory" 打亂字母順序會變成 "dirtyroom" , "tea" 會變成"eat"。
迴文構詞法有一個特點:單詞裡的字母的種類和數目沒有改變,只是改變了字母的排列順序。
因此,將幾個單詞按照字母順序排序後,若它們相等,則它們屬於同一組 anagrams 。

思路:

可以使用unordered_map<string,vector<string> >把Anagram單詞排序後的單詞作為鍵,排序前的單詞作為值,這些值放到一個容器中,即一個鍵對應多個值。如果這個值內單詞的個數>1,說明存在Anagram單詞,則加入到結果容器中。如果=1,說明不存在對應的Anagram詞

程式碼:

class Solution {
public:
    vector<string> anagrams(vector<string> &strs) 
    {
        vector<string> result;//儲存結果
        unordered_map<string,vector<string> > groups;//鍵值對
		//先把鍵值對對應起來
        for(int i = 0 ; i < strs.size() ; i++)
        {
            string word = strs[i];
            sort(word.begin(),word.end());//對當前單詞排序
            groups[word].push_back(strs[i]);//插入鍵值對
        }
		//統計值中單詞的個數是否>1,如果>1則把這個容器值插入到result的尾部
        for(unordered_map<string,vector<string> >::iterator iter = groups.begin() ; iter != groups.end() ; iter++)
        {
            if(iter->second.size() > 1)
            {
                result.insert(result.end(),iter->second.begin(),iter->second.end());
            }
        }
        return result;
    }
};



相關推薦

LeetCode OJ Anagrams 構詞法

題目: Given an array of strings, return all groups of strings that are anagrams.(給定一組字串,返回符合迴文構詞法的單片語,即如果某幾個單詞是迴文單詞,則放入一個容器中並返回)。 Note:

LeetCode 9. Palindrome Number

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 O

LeetCode oj 409. Longest Palindrome (

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is

LeetCode OJ Palindrome Number數字

題目:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie,

LeetCode刷題筆記-009:palindrome number

題目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Exa

LeetCode刷題——第九題

9.迴文數 題目描述 思路 程式碼示例 題目描述 判斷一個整數是否是迴文數。迴文數是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數。 示例 1: 輸入: 121 輸出: true 示例 2:

LeetCode--647. Palindromic Substrings字串個數Python

題目: 給定一個字串,計算該字串中有多少迴文子字串。 解題思路: 動態規劃,類似之前的一個題目,直接給連結和程式碼 http://blog.csdn.net/xiaoxiaoley/article/details/77407263 程式碼(Python): class So

LeetCode | Palindrome Number數字

Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra

LeetCode OJ 4Sum ”四個數的和“

題目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d

LeetCode 9. Palindrome Number

num bre spa new NPU amp public center integer Determine whether an integer is a palindrome. An integer is a palindrome when it reads the

9. Palindrome NumberC++

將int轉換為string,注意for迴圈判斷條件的簡化 class Solution { public: bool isPalindrome(int x) { if(x < 0) return false; string s =

計蒜客 2018南京網路賽 I Skr

題目:給一串由0..9組成的數字字串,求所有不同迴文串的權值和。比如說“1121”這個串中有“1”,“2”,“11”,“121”三種迴文串,他們的權值分別是1,2,11,121。最終輸出ans=135。 思路:之前寫了馬拉車的演算法,網上看到的這個題是迴文樹的模板題。。。

ACM-ICPC 2018南京賽區網路賽 I.Skr 自動機

計蒜客開了禁止複製,我掛了破解網頁限制的指令碼能複製但是不太好看,就不貼題目了。 題目大意是給出一個數字串,求其本質不同的迴文子串的和。 一開始沒看到“本質不同”,上來就想跑馬拉車 + Sam,

17E E. Palisection

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you

HDU5157 Harry and magic string

題意:從一個串s中取兩個迴文子串,求使兩個迴文串互不相交的取法數。 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm&g

【APIO2014】—自動機

傳送門 迴文自動機簡單題 因為不想學字尾自動機做法,於是去大概把迴文自動機學了 感覺比字尾自動機簡單多了啊 很顯然就是 f a

bzoj4044: [Cerc2014] Virus synthesis自動機

題目 學習部落格 題解 #include<bits/stdc++.h> using namespace std; const int N=100002; int mp[99],ans,T,f[N],fail[N],ch[N][4],sz,q[N],h,t,len[N],s[N

備戰藍橋杯真題第五屆第三題素數

                                   迴文素數10301是個5位的素數。它有個特點,把數字倒過來還是它本身,具有這樣特徵的素數,我們稱之為:迴文素數。105011060111311這些都是5位的迴文素數。請你計算一下,像這樣的5位數的迴文素數,

LeetCode OJ 3Sum (三個數的和

題目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array whi

LeetCode OJ演算法題三十五:Valid Sudoku

題目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty