Given a non-empty string s and an abbreviation ABBR,returnwhether 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 is only the above abbreviations is valid abbreviations of the string"Word". Any and string is not a valid abbreviation of "word". Note:assume s contains only lowercase letters and ABBR contains only lowercase letters and digits. Example1: Given s= "Internationalization", abbr = "i12iz4n": Returntrue. Example2: Given s= "Apple", abbr = "a2e": Returnfalse.
1 Public classSolution {2 Public Booleanvalidwordabbreviation (string Word, String abbr) {3 inti = 0, j = 0;4 while(I<word.length () && j<abbr.length ()) {5 if(!IsDigit (Abbr.charat (j))) {6 if(Word.charat (i)! = Abbr.charat (j))return false;7i++;8J + +;9 }Ten Else { One intnum = 0; A while(J<abbr.length () &&IsDigit (Abbr.charat (j))) { -num = num*10 + (int) (Abbr.charat (j)-' 0 '); - if(num = = 0)return false;//"001" with ' 0 ' at front should return false theJ + +; - } -i = i +num; - } + } - if(I==word.length () && j==abbr.length ())return true; + return false; A } at - Public BooleanIsDigit (Charc) { - if(c>= ' 0 ' && c<= ' 9 ')return true; - return false; - } -}
Leetcode:valid Word abbreviation