Lintcode634 Word Squares

來源:LintCode

Given a set of words without duplicates, find all word squares you can build from them.

A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns).

For example, the word sequence ["ball","area","lead","lady"] forms a word square because each word reads the same both horizontally and vertically.

b a l l
a r e a
l e a d
l a d y

可以發(fā)現(xiàn)規(guī)律就是找到第一個string后浪听,第二個string是以第一個string第二個字母開頭蚯斯,第三個string是以前兩個string的第三個字母開頭火的,第四個string是以前三個string第四個字母開頭

所以每次新加進(jìn)list<string>的單詞都是以list現(xiàn)有的string.charAt(list.size()) 為開頭

本題應(yīng)用到了字典樹嘹叫,樹內(nèi)有26個字母和此時(shí)以此字母開始的單詞。

整體結(jié)構(gòu)就是開始遍歷words, 把每個word加入到一個list里,然后以此展開深搜尋找呻澜,每次都是以現(xiàn)有的prefix來在字典尋找下一個可能的單詞纠屋。一旦list<string> 的單詞數(shù)目達(dá)到單詞長度的級別涂臣,那么一個square形成。 代碼如下

class Solution {
   public List<List<String>> wordSquares(String[] words) {
        // write your code here
        Trie t = new Trie();
        
        for(String str: words){
            Build(t, str);
        }
        
        List<List<String>> ans = new ArrayList<>();
        int len = words[0].length();
        

        for(int i = 0; i < words.length; i++){
            List<String> ls = new ArrayList<>();
            ls.add(words[i]);
           
            search(len, t, ans, ls);
        }

        return ans;

        
    }
    private void search(int len, Trie tr, List<List<String>> ans,
            List<String> ansBuilder) {
        if (ansBuilder.size() == len) {
            ans.add(ansBuilder);
            return;
        }

        int idx = ansBuilder.size();
        StringBuilder prefixBuilder = new StringBuilder();
        for (String s : ansBuilder)
            prefixBuilder.append(s.charAt(idx));
        List<String> startWith = findPrefix(tr, prefixBuilder.toString());
        for (String sw : startWith) {
            List<String> next = new ArrayList<>(ansBuilder);
            next.add(sw);
            search(len, tr, ans, next);
        }
    }
    
    private void Build(Trie t, String word){
        Trie p = t;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if(p.tries[index] == null){
                p.tries[index] = new Trie();
            }
            p.tries[index].startWith.add(word);
            
           
            p = p.tries[index];
        }
    }
    public List<String> findPrefix(Trie t, String prefix){
        Trie root = t;
        List<String> ans = new ArrayList<>();
        for(int i = 0; i < prefix.length(); i++){
            int index = prefix.charAt(i) - 'a';
            if(root.tries[index] == null){
                return ans;
            }
            root = root.tries[index];
        }
        
         ans.addAll(root.startWith);
        return ans;
    }
}
class Trie{
    List<String> startWith = new ArrayList<>();
    Trie[] tries = new Trie[26];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末售担,一起剝皮案震驚了整個濱河市赁遗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌族铆,老刑警劉巖岩四,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哥攘,居然都是意外死亡剖煌,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門献丑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來末捣,“玉大人,你說我怎么就攤上這事创橄÷嶙觯” “怎么了?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵妥畏,是天一觀的道長邦邦。 經(jīng)常有香客問我,道長醉蚁,這世上最難降的妖魔是什么燃辖? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮网棍,結(jié)果婚禮上黔龟,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好氏身,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布巍棱。 她就那樣靜靜地躺著,像睡著了一般蛋欣。 火紅的嫁衣襯著肌膚如雪航徙。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天陷虎,我揣著相機(jī)與錄音到踏,去河邊找鬼。 笑死尚猿,一個胖子當(dāng)著我的面吹牛窝稿,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播谊路,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼讹躯,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缠劝?” 一聲冷哼從身側(cè)響起潮梯,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎惨恭,沒想到半個月后秉馏,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡脱羡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年萝究,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片锉罐。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡帆竹,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出脓规,到底是詐尸還是另有隱情栽连,我是刑警寧澤,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布侨舆,位于F島的核電站秒紧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏挨下。R本人自食惡果不足惜熔恢,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望臭笆。 院中可真熱鬧叙淌,春花似錦秤掌、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至衅谷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間似将,已是汗流浹背获黔。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留在验,地道東北人玷氏。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像腋舌,于是被迫代替她去往敵國和親盏触。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354