【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 Visual code安装与使用
1.直接启动软件中心,输入visual studio code,点击install即可,千万千万不要去装逼搞什么linux指令安装,死都不知道怎么死的 2.Visual code是以文件夹为工程目录的 ...
- SpringMvc的过滤器。
一:过滤器的原理: 过滤器放在web资源之前,可以在请求抵达它所应用的web资源(可以是一个Servlet.一个Jsp页面,甚至是一个HTML页面)之前截获进入的请求,并且在它返回到客户之前截获输出请 ...
- CoffeeScript编写简单新闻页(仅UI)
CoffeeScript编写简单新闻页(仅UI) 1. 配置(在公司搭建好的环境下配置) omnisocials-backend/src/backend/modules/member/config/m ...
- [CSP-S模拟测试]:施工(DP+单调栈+前缀和)
题目描述 小$Y$家门前有一条街道,街道上顺序排列着$n$幢建筑,其中左起第$i$幢建筑的高度为$h_i$.小$Y$定义街道的不美观度为所有相邻建筑高度差的绝对值之和乘上常数$c$,为了改善街道环境, ...
- JS基础入门篇(四)—this的使用,模拟单选框,选项卡和复选框
1.this的使用 this js中的关键字 js内部已经定义好了,可以不声明 直接使用 this的指向问题 1. 在函数外部使用 this指向的是window 2. 在函数内部使用 有名函数 直接调 ...
- 实验三 《敏捷开发与XP实践》实验报告
一.实验内容 任务一 1.参考 http://www.cnblogs.com/rocedu/p/6371315.html#SECCODESTANDARD 安装alibaba 插件,解决代码中的规范问题 ...
- Redis入门很简单之七【使用Jedis实现客户端Sharding】
Redis入门很简单之七[使用Jedis实现客户端Sharding] 博客分类: NoSQL/Redis/MongoDB redisjedisspringsharding分片 <一>. 背 ...
- Class-DbConnectionManipulator:Execute,QueryFirstDefault<>
ylbtech-Class-DbConnectionManipulator:Execute,QueryFirstDefault<> 1.返回顶部 1. public object GetS ...
- token是什么?和session什么区别,怎么用
对于初学者来说,对Token和Session的使用难免会限于困境,开发过程中知道有这个东西,但却不知道为什么要用他?更不知道其原理,今天我就带大家一起分析分析这东西. 一.我们先解释一下他的含义: 1 ...
- 洛谷P3366 【模板】最小生成树(LCT)
[模板]最小生成树 题目传送门 解题思路 用LCT来维护最小生成树. 除了把各顶点作为节点外,每条边也都视为一个节点.对于要加入的边\(e\),检查其两顶点\(x\)和\(y\)是否在同一棵树中,如果 ...