408. Valid Word Abbreviation有效的单词缩写
[抄題]:
Given a?non-empty?string?s?and an abbreviation?abbr, return whether the string matches with the given abbreviation.
A string such as?"word"?contains only the following valid abbreviations:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]Notice that only the above abbreviations are valid abbreviations of the string?"word". Any other string is not a valid abbreviation of?"word".
Note:
Assume?s?contains only lowercase letters and?abbr?contains only lowercase letters and digits.
Example 1:
Given s = "internationalization", abbr = "i12iz4n":Return true.?
Example 2:
Given s = "apple", abbr = "a2e":Return false.?[暴力解法]:
時間分析:
空間分析:
?[優化后]:
時間分析:
空間分析:
[奇葩輸出條件]:
[奇葩corner case]:
[思維問題]:
有兩個單詞居然會想不出兩個指針嗎?
[一句話思路]:
[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
? [五分鐘肉眼debug的結果]:
[總結]:
兩個單詞就用兩個指針,兩個起點
[復雜度]:Time complexity: O() Space complexity: O()
[英文數據結構或算法,為什么不用別的數據結構或算法]:
ASCII碼表的順序是(按二進制排序):數字-大寫字母-小寫字母
[關鍵模板化代碼]:
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
527.?Word Abbreviation 自制單詞壓縮:要排序
411.?Minimum Unique Word Abbreviation 排列組合找出第一個單詞的所有縮寫:回溯法
?[代碼風格] :
class Solution {public boolean validWordAbbreviation(String word, String abbr) {//iniint i = 0, j = 0;//while loopwhile (i < word.length() && j < abbr.length()) {//if letters, go onif (word.charAt(i) == abbr.charAt(j)) {++i;++j;continue;}//cc, first num shouldn't be 0if (abbr.charAt(j) <= '0' || abbr.charAt(j) > '9') return false;//substring of jint start = j;//control j wheneverwhile (j < abbr.length() && abbr.charAt(j) >= '0' && abbr.charAt(j) <= '9') {++j;}int num = Integer.valueOf(abbr.substring(start, j));//add to ii += num;}//returnreturn (i == word.length()) && (j == abbr.length());} } View Code?
轉載于:https://www.cnblogs.com/immiao0319/p/8651383.html
總結
以上是生活随笔為你收集整理的408. Valid Word Abbreviation有效的单词缩写的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: P1586 四方定理
- 下一篇: SQL 注入 OrderBy/0ctf