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. Caused by: org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = integer

    Springboot项目,使用postgresql数据库,mybatis做持久层框架, <select id="select" resultMap="BaseRes ...

  2. 洛谷P1098 字符串的展开【字符串】【模拟】

    题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母或数 ...

  3. BZOJ 2457 - 双端队列 - [思维题]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2457 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. ...

  4. [转载]Invalid bound statement (not found): com.taotao.mapper.TbItemMapper.selectByExample: 错误

    因碰到同样的问题,使用该方法对我有效,为方便以后查找,所以做了转载,原文请查看:https://www.cnblogs.com/fifiyong/p/5795365.html 在Maven工程下,想通 ...

  5. TF模型训练中注意Loss和F1的变化情况

    之前训练模型,认为网络图构建完成,Loss肯定是呈现下降的,就没有太留心,知识关注F1的变化情况,找到最优的F1训练就停止了,认为模型就ok. 但实际中发现,我们要时刻关注网络的损失变化情况,batc ...

  6. Mac操作的一些技能

    截图功能 通常会用到以下快捷键组合: Command+Shift+3截全屏. Command+Shift+4截屏幕选定部分. Command+Shift+4+空格截取所选窗口 在终端输入如下命令,开启 ...

  7. deepin修改javahome不生效,一直显示openjdk解决

    删除/usr/bin/java即可 terwer@terwer-PC:~$ sudo rm /usr/bin/java [sudo] terwer 的密码: terwer@terwer-PC:~$ j ...

  8. bitmq集群高可用测试

    Rabbitmq集群高可用 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模 ...

  9. php之memcached存储session配置、存储、获取

    [session] ①.session.save_handler = memcache session.save_handler 定义了来存储和获取与会话关联的数据的处理器的名字,默认是files ② ...

  10. influxdb服务器 relay

    https://www.influxdata.com/time-series-platform/influxdb/ https://www.xusheng.org/blog/2016/07/30/pe ...