【leetcode】925.Long Pressed Name
题目如下:
Your friend is typing his
nameinto a keyboard. Sometimes, when typing a characterc, the key might get long pressed, and the character will be typed 1 or more times.You examine the
typedcharacters of the keyboard. ReturnTrueif 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: trueExample 4:
Input: name = "laiden", typed = "laiden"
Output: true
Explanation: It's not necessary to long press any character.Note:
name.length <= 1000typed.length <= 1000- The characters of
nameandtypedare 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的更多相关文章
- 【LeetCode】925. Long Pressed Name 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 参考资料 日期 题目地址:https://leetc ...
- 【Leetcode_easy】925. Long Pressed Name
problem 925. Long Pressed Name solution1: class Solution { public: bool isLongPressedName(string nam ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
随机推荐
- Ubuntu分区小知识与分区方案
Most PC operating systems still work with an ancient disk partition scheme that historically makes d ...
- Nmon监控服务端性能
一.安装1.查看服务器操作系统的版本信息 lsb_release -a cat /etc/*release2.下载 a.nmon下载地址:http://nmon.sourceforge.net/pmw ...
- 【CF1238E】Keyboard Purchase(状压DP,贡献)
题意:有m种小写字符,给定一个长为n的序列,定义编辑距离为序列中相邻两个字母位置差的绝对值之和,其中字母位置是一个1到m的排列 安排一种方案,求编辑距离最小 n<=1e5,m<=20 思路 ...
- python-zx笔记2-help
在cmd运行 查看模块的方法: help—查看模块的函数 1 help()
- js策略模式vs状态模式
一.策略模式 1.定义:把一些小的算法,封装起来,使他们之间可以相互替换(把代码的实现和使用分离开来)2.利用策略模式实现小方块缓动 html代码: <div id="containe ...
- Java程序执行的过程
ava程序执行的过程: Step1:将字节码加入内存: Step2:对字节码进行合法性检查: Step3:jvm会为每个字节码文件都生成一个对象(class): Step4:执行静态代码块,初始化静态 ...
- meta标签 使用说明(http-equiv、refresh、seo)
meta标签 使用说明(http-equiv.refresh.seo) meta标签,是在head标签里面,一般用做页面描述的.它的内容,用来描述页面一些信息的,如类型.编码.作者.简介等!虽然,它不 ...
- webpack 4.+版本需要注意的地方
在webpack打包的时候需要npm下载webpack-cli,而且打包JS的命令从以前的webpack .\src\main.js .\dist\boundle.js 要编程 webpack .\ ...
- GET和POST的数据传递到底有何区别?
1. GET和POST与数据如何传递没有关系 GET和POST是由HTTP协议定义的.在HTTP协议中,Method和Data(URL, Body, Header)是正交的两个概念,也就是说,使用哪个 ...
- day 52协程
协程进程线程: # 进程 启动多个进程 进程之间是由操作系统负责调用 # 线程 启动多个线程 真正被CPU执行的最小单位实际是线程 # 开启一个线程 创建一个线程 寄存器 堆栈 # 关闭一个线程 # ...