首页 > 其他 > 详细

[Algo] 292. String Abbreviation Matching

时间:2020-02-20 13:06:03      阅读:63      评论:0      收藏:0      [点我收藏+]

Word “book” can be abbreviated to 4, b3, b2k, etc. Given a string and an abbreviation, return if the string matches the abbreviation.

Assumptions:

  • The original string only contains alphabetic characters.
  • Both input and pattern are not null.
  • Pattern would not contain invalid information like "a0a","0".

Examples:

  • pattern “s11d” matches input “sophisticated” since “11” matches eleven chars “ophisticate”.
public class Solution {
  public boolean match(String input, String pattern) {
    // Write your solution here
    int iIndex = 0;
    int pIndex = 0;
    char[] charArr = pattern.toCharArray();
    while (iIndex < input.length() && pIndex < pattern.length()) {
      char cur = charArr[pIndex];
      if (Character.isLetter(cur)) {
        if (cur != input.charAt(iIndex)) {
          return false;
        }
        iIndex += 1;
        pIndex += 1;
      } else if (Character.isDigit(cur)) {
        int num = 0;
        while (pIndex < charArr.length && Character.isDigit(charArr[pIndex])) {
          num = 10 * num + (int)(charArr[pIndex] - ‘0‘);
          pIndex += 1;
        }
        iIndex += num;
      }
    }
    return iIndex == input.length() && pIndex == pattern.length();
  }
}

 

[Algo] 292. String Abbreviation Matching

原文:https://www.cnblogs.com/xuanlu/p/12334831.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!