Problem
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.
Solutionclass Solution { public boolean validWordAbbreviation(String word, String abbr) { if (word == null || abbr == null) return false; int i = 0, j = 0; while (i < word.length() && j < abbr.length()) { char ch1 = word.charAt(i), ch2 = abbr.charAt(j); if (ch1 == ch2) { i++; j++; } else if (ch2 >= "1" && ch2 <= "9") { int start = j; while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) j++; int count = Integer.valueOf(abbr.substring(start, j)); i += count; } else return false; } return i == word.length() && j == abbr.length(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/72242.html
Given s = internationalization, abbr = i12iz4n: Return true. Given s = apple, abbr = a2e: Return false. public class Solution { public boolean validWordAbbreviation(String word, String abbr) { ...
摘要:鏈接注意第一個數(shù)字是的情況,這種也是不合法的。還有一個注意的就是要想和有相同的縮寫,長度必須和它相同,所以只保留長度相同的。注意剪枝,當(dāng)前長度已經(jīng)超過就不需要繼續(xù)了。二進(jìn)制的做法是這樣的,先對字典里面的單詞進(jìn)行處理。 Valid Word Abbreviation 鏈接:https://leetcode.com/problems... 注意第一個數(shù)字是0的情況,[a, 01]這種也是不...
摘要:分析這道題第一步一定要理解題意,首先要考慮的是會有多少種結(jié)果。仔細(xì)觀察會發(fā)現(xiàn),最終會有種結(jié)果。然后就很顯然應(yīng)該用每次存下當(dāng)前結(jié)果,然后繼續(xù)。 Generalized Abbreviation Write a function to generate the generalized abbreviations of a word. Example:Given word = word, ...
摘要:題目鏈接要輸出所有的結(jié)果,標(biāo)準(zhǔn)思路。也可以做,保留為,改為數(shù)字的為,然后結(jié)果就是這么多,每個數(shù)學(xué)遍歷一遍求對應(yīng)的即可。 320. Generalized Abbreviation 題目鏈接:https://leetcode.com/problems... 要輸出所有的結(jié)果,backtracking標(biāo)準(zhǔn)思路。 public class Solution { public List...
摘要:題目內(nèi)容這題也是鎖住的,通過率只有左右。另外,字典里面只有兩個的時候,也是返回。最后再說兩句距離上一篇文章過了一段時間了,這段時間搬家再適應(yīng)新環(huán)境,解決心理問題。 題目內(nèi)容 An abbreviation of a word follows the form . Below are some examples of word abbreviations: a) it ...
閱讀 3426·2021-11-15 11:39
閱讀 1569·2021-09-22 10:02
閱讀 1317·2021-08-27 16:24
閱讀 3603·2019-08-30 15:52
閱讀 3417·2019-08-29 16:20
閱讀 831·2019-08-28 18:12
閱讀 555·2019-08-26 18:27
閱讀 725·2019-08-26 13:32