题目如下:

Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.

You examine the typed characters of the keyboard.  Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.

Example 3:

Input: name = "leelee", typed = "lleeelee"
Output: true

Example 4:

Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.

Note:

  1. name.length <= 1000
  2. typed.length <= 1000
  3. The characters of name and typed are lowercase letters.

解题思路:我的方法是把字符串解析成 [字符,该字符连续出现的次数]的格式,例如lleeelee解析的结果是 ['l','2','e','3','l','1','e','2'],最后只要比较name的typed的解析结果即可,比较的方法是字符必须相同,数字必须name <= typed。

代码如下:

class Solution(object):
def splitStr(self,sval):
lsplit = []
lastChar = None
lastCount = 0
for i in sval:
if lastChar == None:
lastChar = i
lastCount = 1
elif lastChar != i:
lsplit.append(lastChar)
lsplit.append(str(lastCount))
lastChar = i
lastCount = 1
else:
lastCount += 1
lsplit.append(lastChar)
lsplit.append(str(lastCount))
return lsplit
def isLongPressedName(self, name, typed):
"""
:type name: str
:type typed: str
:rtype: bool
"""
nsplit = self.splitStr(name)
tsplit = self.splitStr(typed)
if len(nsplit) != len(tsplit):
return False
for v1,v2 in zip(nsplit,tsplit):
if v1.isdigit() and v2.isdigit():
if int(v1) > int(v2):
return False
elif v1 != v2:
return False
return True

【leetcode】925.Long Pressed Name的更多相关文章

  1. 【LeetCode】925. Long Pressed Name 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...

  2. 【Leetcode_easy】925. Long Pressed Name

    problem 925. Long Pressed Name solution1: class Solution { public: bool isLongPressedName(string nam ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  5. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  8. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  9. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

随机推荐

  1. JAVA工具类--手机号生成与正则校验

    package utils; import java.util.Random; import java.util.regex.Pattern; /** * Created with IntelliJ ...

  2. CentOS7安装配置redis5.0.5

    一.安装必需包gcc yum install gcc 二.下载redis,并解压 wget http://download.redis.io/releases/redis-5.0.5.tar.gz t ...

  3. Linux系统重要文件(三)

    一系统运行级别文件 文件路径:/etc/inittab 文件作用说明:定义系统启动后,自动开启哪些软件程序系统 runlevel 查看当前运行级别 centos6系统运行级别: 7个级别      0 ...

  4. [CF852H]Bob and stages

    题意:给出平面上\(n\)个点,要求选出\(k\)个点,使得这些点形成一个凸包,且凸包内部没有点,求最大面积.无解输出\(0\). 题解:枚举凸包最左的点\(p\),删除所有在\(p\)左边的点,然后 ...

  5. vue中img图片加载中以及加载失败显示默认图片问题

    加载中默认图片:主要是onload事件监听,data中定义变量 imgSrc :require('./default.png'): <div class="per-pic" ...

  6. fiddler使用笔记1

    转载地址:写得很不错的fildder教程   http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站:  www.fiddler2.c ...

  7. 0.tensorflow——常用说明

    1.tf tf.placeholder(tf.float32,shape=[None,inputSize])#类似于一个float32的声明 tf.reduce_mean()#求均值,可以是不同维度 ...

  8. svn设置文件提交过滤、svn设置classes文件提交

    在svn提交文件的时候为了避免一些不必要的文件也提交到资源库 像编译后的.class文件 第一步:在文件中右击打开设置, 第二步:找到全局忽略样式 第三步:修改要过滤的文件 设置过滤通配符 *clas ...

  9. MSF——Payload模块(二)

    MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) 一.exploit和payload e ...

  10. java的实用类

    1)   Random类 用于生成随机数字,所有生成的数字,都是等概率的. nextInt():生成的值介于int的所有取值范围(-231 ~ 231-1) nextInt(int value):生成 ...