Leetcode:valid Word abbreviation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.