467. Unique Substrings in Wraparound String

題目

Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".
Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s.
Note: p consists of only lowercase English letters and the size of p might be over 10000.

Example 1:
Input: "a"
Output: 1
Explanation: Only the substring "a" of string "a" is in the string ?s.
Example 2:
Input: "cac"
Output: 2
Explanation: There are two substrings "a", "c" of string "cac" in the string s.
Example 3:
Input: "zab"
Output: 6
Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.

分析

已知有一個無限循環(huán)的字符串s: "abc...xyz"+"abc..xyz"...+...,給你一個字符串奶卓,問這個字符串有多少個子串在s中擂送。
這道題開始想用兩個指針做非迹,但是會引起重復顿仇,比如cac中會得到c 和 ac 和c温数。
其實應該使用動態(tài)規(guī)劃宙彪,代碼很好寫,思路不好想等限。
得到以每個字符結尾的最大合乎要求的長度爸吮,加起來就是最終結果
比如abcd以d結尾,長度為4望门,則有abcd,bcd,cd,d這四個子串形娇,再加上以a、b筹误、c結尾的桐早,就是最終結果,而且取最長的可以避免重復厨剪。

The idea is, if we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z', then the summary of them is the answer. Why is that?

1. The max number of unique substring ends with a letter equals to the length of max contiguous substring ends with that letter. Example "abcd", the max number of unique substring ends with 'd' is 4, apparently they are "abcd", "bcd", "cd" and "d".
2. If there are overlapping, we only need to consider the longest one because it covers all the possible substrings. Example: "abcdbcd", the max number of unique substring ends with 'd' is 4 and all substrings formed by the 2nd "bcd" part are covered in the 4 substrings already.
3. No matter how long is a contiguous substring in p, it is in s since s has infinite length.
4. Now we know the max number of unique substrings in p ends with 'a', 'b', ..., 'z' and those substrings are all in s. Summary is the answer, according to the question.

代碼

public int findSubstringInWraproundString(String p) {
    // count[i] is the maximum unique substring end with ith letter.
    // 0 - 'a', 1 - 'b', ..., 25 - 'z'.
    int[] count = new int[26];
    
    // store longest contiguous substring ends at current position.
    int maxLengthCur = 0; 

    for (int i = 0; i < p.length(); i++) {
        if (i > 0 && (p.charAt(i) - p.charAt(i - 1) == 1 || (p.charAt(i - 1) - p.charAt(i) == 25))) {
            maxLengthCur++;
        }
        else {
            maxLengthCur = 1;
        }
        
        int index = p.charAt(i) - 'a';
        count[index] = Math.max(count[index], maxLengthCur);
    }
    
    // Sum to get result
    int sum = 0;
    for (int i = 0; i < 26; i++) {
        sum += count[i];
    }
    return sum;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末哄酝,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子祷膳,更是在濱河造成了極大的恐慌陶衅,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件直晨,死亡現場離奇詭異搀军,居然都是意外死亡,警方通過查閱死者的電腦和手機勇皇,發(fā)現死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門罩句,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人敛摘,你說我怎么就攤上這事门烂。” “怎么了着撩?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵诅福,是天一觀的道長。 經常有香客問我拖叙,道長氓润,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任薯鳍,我火速辦了婚禮咖气,結果婚禮上挨措,老公的妹妹穿的比我還像新娘。我一直安慰自己崩溪,他們只是感情好浅役,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著伶唯,像睡著了一般觉既。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乳幸,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天瞪讼,我揣著相機與錄音,去河邊找鬼粹断。 笑死符欠,一個胖子當著我的面吹牛,可吹牛的內容都是我干的瓶埋。 我是一名探鬼主播希柿,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼养筒!你這毒婦竟也來了曾撤?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤闽颇,失蹤者是張志新(化名)和其女友劉穎盾戴,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體兵多,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡尖啡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了剩膘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片衅斩。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖怠褐,靈堂內的尸體忽然破棺而出畏梆,到底是詐尸還是另有隱情,我是刑警寧澤奈懒,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布奠涌,位于F島的核電站,受9級特大地震影響磷杏,放射性物質發(fā)生泄漏溜畅。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一极祸、第九天 我趴在偏房一處隱蔽的房頂上張望慈格。 院中可真熱鬧怠晴,春花似錦、人聲如沸浴捆。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽选泻。三九已至冲粤,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間滔金,已是汗流浹背色解。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留餐茵,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓述吸,卻偏偏與公主長得像忿族,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子蝌矛,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容