zoukankan      html  css  js  c++  java
  • [Algo] 292. String Abbreviation Matching

    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();
      }
    }
  • 相关阅读:
    判断质数
    猜拳三局两胜
    输入年月日判断是这一年的哪一天
    switch case
    猜拳
    判断年月日是否正确
    老狼老狼几点了
    判断是否中奖
    平滑部署war包到tomcat-deploy.sh
    只用120行Java代码写一个自己的区块链-3挖矿算法
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12334831.html
Copyright © 2011-2022 走看看