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();
      }
    }
  • 相关阅读:
    网络安全之常见攻击
    引入element-ui
    引入sass
    浏览器解析流程
    JDK8 HashMap--removeNode()移除节点方法
    JDK8 HashMap--treeify()树形化方法
    JDK1.8 HashMap--treeifyBin()方法
    二叉查找树ADT
    队列ADT

  • 原文地址:https://www.cnblogs.com/xuanlu/p/12334831.html
Copyright © 2011-2022 走看看