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:

["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:

Given s = "internationalization", abbr = "i12iz4n":

Return true.

Example 2:

Given s = "apple", abbr = "a2e":

Return false.

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

Code

class Solution:
def validWordAbbreviation(self, word, abbr):
nums, lw, la, pw, pa = "", len(word), len(abbr),0,0
while pw < lw and pa < la:
num = ""
while abbr[pa] in nums:
num += abbr[pa]
if num[0] == '': return False
pa += 1
if pa == la:
return len(word[pw:]) == int(num)
if not num:
if word[pw] != abbr[pa]:
return False
else:
pw += 1
pa += 1
else:
pw += int(num)
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. hadoop Codec

  2. Linux用户及文件权限管理

    Linux用户及文件权限管理

  3. Python与金融量化分析----金融与量化投资

    一:金融了解 金融:就是对现有资源进行重新的整合之后,进行价值和利润的等效流通. 金融工具: 股票 期货 黄金 外汇 基金 ............. 股票: 股票是股份公司发给出资人多的一种凭证,股 ...

  4. tfidf_CountVectorizer 与 TfidfTransformer 保存和测试

    做nlp的时候,如果用到tf-idf,sklearn中用CountVectorizer与TfidfTransformer两个类,下面对和两个类进行讲解 一.训练以及测试 CountVectorizer ...

  5. Scss基础用法

    Scss基础用法 一.注释用法: (1)//comment:该注释只是在.scss源文件中有,编译后的css文件中没有. (2)/! /:重要注释,任何style的css文件中都会有,一般放置css文 ...

  6. PCIe 复位:Clod reset、warm reset、Hot reset、Function level reset

    2015年09月06日 17:06:01 yijingjing17 阅读数:9029 标签: PCIEReSet复位Clod resetwarm reset 更多 个人分类: PCIe        ...

  7. 用NFS挂载root出现:NFS: failed to create MNT RPC client, status=-101(-110)

      2014-02-18 08:06:17 By Ly #Linux 阅读(78) 评论(0) 错误信息如下: Root-NFS: nfsroot=/home/zenki/nfs/rootfs NFS ...

  8. PHP之数组函数

    php数组中的预定义变量 预定义常量 CASE_LOWER (integer) CASE_LOWER 用在array_change_key_case()中将数组的键名转换为小写字母.这也是array_ ...

  9. (未完成)在block内如何修改block外部变量

    变量必须用__block修饰,否则编译不通过 block内部会把变量拷贝到堆区 变量从栈区copy->堆区 通过对对象取地址,打印出对象在内存中的地址 &a block不允许修改外部变量 ...

  10. python实现单链表反转(经典笔试题)

    https://blog.csdn.net/su_bao/article/details/81072849 0.说在前面的话 链表结构,说难不难,说易不易,一定要亲自编程实现一下.其次就是一定要耐心, ...