[經典面試題][百度]尋找兄弟單詞,試題兄弟

來源:互聯網
上載者:User

[經典面試題][百度]尋找兄弟單詞,試題兄弟

題目

一個單詞單詞字母交換,可得另一個單詞,如army->mary,成為兄弟單詞。提供一個單詞,在字典中找到它的兄弟。描述資料結構和查詢過程。

思路一
兄弟單詞必須滿足字元相同,相同字元個數也必須相同。基於這點用Hash實現:
(1)申請一個int數組 count[26]用來統計每個字元出現的次數。
(2)掃描字典中的單詞,只考慮長度和給出單詞長度一樣大小的單詞(長度不同肯定不是兄弟單詞),用broCount[26]來統計單詞中每個字元出現的次數。
(3)如果count和broCount一樣,表示是兄弟單詞否則不是。

代碼一

    /*---------------------------------------------    *   日期:2015-02-22    *   作者:SJF0115    *   題目: 兄弟單詞    *   來源:百度    *   部落格:    -----------------------------------------------*/    #include <iostream>    #include <vector>    #include <cstring>    #include <algorithm>    using namespace std;    class Solution {    public:        // dic字典集合 word尋找單詞        vector<string> BrotherWord(vector<string> dic,string word){            int count[26];            vector<string> brother;            // 初始化            memset(count,0,sizeof(count));            int wordSize = word.size();            int dicSize = dic.size();            // 統計字元以及個數            for(int i = 0;i < wordSize;++i){                ++count[word[i]-'a'];            }//for            // 遍曆字典            for(int i = 0;i < dicSize;++i){                if(IsBrother(word,count,dic[i])){                    brother.push_back(dic[i]);                }//if            }//for            return brother;        }    private:        bool IsBrother(string word,int count[],string broWord){            int size = broWord.size();            int wordSize = word.size();            // 長度不一樣肯定不是兄弟單詞            if(size != wordSize){                return false;            }//if            // 統計字元及個數            int broCount[26];            memset(broCount,0,sizeof(broCount));            for(int i = 0;i < size;++i){                ++broCount[broWord[i]-'a'];            }//for            // 只有字元一樣,字元個數一樣才是兄弟單詞            int k;            for(k = 0;k < 26;++k){                // 不是兄弟單詞                if(count[k] != broCount[k]){                    break;                }//if            }//for            if(k == 26){                return true;            }//if            return false;        }    };    int main() {        Solution solution;        vector<string> dic = {"mary","many","book","army","mood","man","doom","mod"};        string word("mary");        vector<string> result = solution.BrotherWord(dic,word);        for(int i = 0;i < result.size();++i){            cout<<result[i]<<" ";        }//for        cout<<endl;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.