作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/contest/weekly-contest-107/problems/long-pressed-name/

题目描述

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.

题目大意

打字输入名字的时候有可能手一滑把某些字符重复输入了,现在想知道输入的这个字符串是否可能由真正的名字打出来。

解题方法

周赛第一题,随时是个easy的题目,但是还是花了半个小时。

思想是,使用两个指针,分别指向名字和输入字符串,然后判断对应位置是否能够对应的上。具体做法是统计两个字符串中相同的字符串重复出现了多少次。我用一个变量指向name,每次向后移动,在每次开始的时候需要保存这个字符,然后我们需要找一下每个字符串后面有多少个相同的字符。最后需要判断,如果输入的这个字符的个数小于名字里面有的,那么就是输入错误了。当所有的判断都结束没有返回错误,那么就是成功了。

时间复杂度是O(N),空间复杂度是O(1).

class Solution(object):
def isLongPressedName(self, name, typed):
"""
:type name: str
:type typed: str
:rtype: bool
"""
M = len(name)
N = len(typed)
i, j = 0, 0
while i < M:
c_i = name[i]
count_i = 0
count_j = 0
while i < M and name[i] == c_i:
i += 1
count_i += 1
while j < N and typed[j] == c_i:
j += 1
count_j += 1
if count_j < count_i:
return False
return True

参考资料

日期

2018 年 10 月 21 日 —— 这个周赛有点难

【LeetCode】925. Long Pressed Name 解题报告(Python)的更多相关文章

  1. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  4. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  5. 【LeetCode】886. Possible Bipartition 解题报告(Python)

    [LeetCode]886. Possible Bipartition 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  6. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

  7. 【LeetCode】870. Advantage Shuffle 解题报告(Python)

    [LeetCode]870. Advantage Shuffle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】593. Valid Square 解题报告(Python)

    [LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】435. Non-overlapping Intervals 解题报告(Python)

    [LeetCode]435. Non-overlapping Intervals 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...

随机推荐

  1. GWAS在农业上应用

    农业的组学技术应用虽然落后于人的研究,这是什么意义的问题,但有时农业基因组有自己无可比拟的优势,那就是材料.下面介绍GWAS应用. GWAS(Genome-wide association study ...

  2. MAC下如何连接安卓(小米)手机进行互传文件?

    命令行: brew cask install android-file-transfer AndroidFileTransfer, 在andorid设备和您的mac电脑之间浏览和传输文件: 不论通过什 ...

  3. 微信小程序调试bug-日程计划类

    首先嘤嘤嘤一下,破bug,改了我一天,摔(′д` )-彡-彡 写的个微信小程序 逻辑如下,正常的功能是,我可以新建,修改,查询(按筛选条件),删除某个日程信息,后面贴个页面,我的bug出现就很搞笑了, ...

  4. 腾讯云联合中国信通院&作业帮等首发《降本之源-云原生成本管理白皮书》

    在11月4日举办的2021腾讯数字生态大会云原生专场上,腾讯云联合中国信通院.作业帮等率先在国内重磅发布了<降本之源-云原生成本管理白皮书>(简称白皮书),基于腾讯云在业内最大规模的 Ku ...

  5. Vue 前端配置多级目录实践(基于Nginx配置方式)

    前情提要 有阵子没更新博客了,因为快年结了工作比较多,这不,最近公司的对外演示环境出现问题这个活儿也落到了我的头上-- 事情是这样的,原来演示环境有很多服务,每个服务都是对外单独开一个端口,比如 ht ...

  6. C语言中的使用内存的三段

    1.正文段即代码和赋值数据段 一般存放二进制代码和常量 2.数据堆段 动态分配的存储区在数据堆段 3.数据栈段 临时使用的变量在数据栈段 典例 若一个进程实体由PCB.共享正文段.数据堆段和数据栈段组 ...

  7. 查看linux系统CPU和内存命令

    cat /proc/cpuinfo查看linux系统的CPU型号.类型以及大小,如下图所示.   通过greap命令根据Physical Processor ID筛选出多核CPU的信息.   cat ...

  8. js处理title超长问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. 【Java 基础】Java Map中的Value值如何做到可以为任意类型的值

    Occasionally the average developer runs into a situation where he has to map values of arbitrary typ ...

  10. Spring Boot中使用Mybatis

    一.步骤 导入依赖:MySQL驱动.Druid依赖.MyBatis与Spring Boot整合依赖.Lombok依赖 在Service接口实现类上添加@Service注解 在Dao接口上添加@Mapp ...