Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

A string such as "word" contains only the following valid abbreviations:

  1. ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".

Note:
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.

Example 1:

  1. Given s = "internationalization", abbr = "i12iz4n":
  2.  
  3. Return true.

Example 2:

  1. Given s = "apple", abbr = "a2e":
  2.  
  3. Return false.

思路就是依次将abbr的数字弄出来, 然后再word上面相加, 否则就比较char, 最后出来loop之后应该是都指到各自的length上面.

Code

  1. class Solution:
  2. def validWordAbbreviation(self, word, abbr):
  3. nums, lw, la, pw, pa = "", len(word), len(abbr),0,0
  4. while pw < lw and pa < la:
  5. num = ""
  6. while abbr[pa] in nums:
  7. num += abbr[pa]
  8. if num[0] == '': return False
  9. pa += 1
  10. if pa == la:
  11. return len(word[pw:]) == int(num)
  12. if not num:
  13. if word[pw] != abbr[pa]:
  14. return False
  15. else:
  16. pw += 1
  17. pa += 1
  18. else:
  19. pw += int(num)
  20. return pw == lw and pa == la

[LeetCode] 408. Valid Word Abbreviation_Easy的更多相关文章

  1. LeetCode 422. Valid Word Square

    原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...

  2. [LeetCode] 422. Valid Word Square_Easy

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  3. 【LeetCode】408. Valid Word Abbreviation 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 题目地址:https://leetcod ...

  4. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  5. 408. Valid Word Abbreviation

    感冒之后 睡了2天觉 现在痊愈了 重启刷题进程.. Google的题,E难度.. 比较的方法很多,应该是为后面的题铺垫的. 题不难,做对不容易,edge cases很多,修修改改好多次,写完发现是一坨 ...

  6. [LeetCode] Valid Word Square 验证单词平方

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  7. Leetcode: Valid Word Square

    Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...

  8. 【LeetCode】422. Valid Word Square 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拼接出每一列的字符串 日期 题目地址:https:// ...

  9. [LeetCode] Minimum Unique Word Abbreviation 最短的独一无二的单词缩写

    A string such as "word" contains the following abbreviations: ["word", "1or ...

随机推荐

  1. DOM元素查找

    一.DOM是document的缩写,他是操作html文档的方法 二.常用查找元素的方法 直接 1.document.getElementById('标签的id')   在html中标签的id是不允许重 ...

  2. ubuntu下安装Python3

    到www.python.org网站下载python3.3.2 Gzipped source tar ball (3.3.2) (sig), ~ 16 MB 解压tar vxzf Python3.3.2 ...

  3. 无法在Web服务器上启动调试。

    Ⅰ x 操作超时 有关详细信息,请单击"帮助" x IIS--应用程序池--找到用到的程序池--回收   2 报这个错误的时候,我的IIS应用程序池只有一个>>> ...

  4. python 同时运行两个程序

    linux的话: python py1.py & python py2/py &

  5. vim中的ctrl+s导致的“假死”、无响应、不接受输入

    有时候vim看到vim的光标在闪烁,但无法输入任何东西,最后只好结束终端了事. 这种现象,是windows用户在使用vim时经常犯的“错误”.在windows下,为了保护自己的劳动成果,ctrl+s已 ...

  6. CodeForces 1099F - Cookies - [DFS+博弈+线段树]

    题目链接:https://codeforces.com/problemset/problem/1099/F Mitya and Vasya are playing an interesting gam ...

  7. [No000011A]Office Excel设置显示日期与星期

    设置excel日期格式,自定义,yyyy-mm-dd 上午/下午 hh:mm:ss AM/PM dddd aaaa

  8. php之二叉树

    二叉树的特点: ①.每个节点最多有两个子树,所以二叉树中不存在度大于2的节点.注意不是只有两个子树,最多有两个子树,没有子树或者只有一颗子树都是可以的. ②左子树和右子树是有顺序的. ③即使树中只有一 ...

  9. [about remote controller]--mstsc-teamviewer-vnc,nomachine

    https://www.jianshu.com/p/c80db368ed8a https://www.nomachine.com/download Ubuntu安装VNC,VNC却无法随系统启动,遂换 ...

  10. [development][profile][dpdk] KK程序性能调优

    KK程序: 1. 两个线程,第一个从DPDK收包,通过一个ring数据传递给第二个线程.第二个线程将数据写入共享内存. 2. 第二个内存在发现共享内存已满时,会直接丢弃数据. 3. 线程二有个选项de ...