博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode刷题:925. Long Pressed Name
阅读量:4040 次
发布时间:2019-05-24

本文共 1554 字,大约阅读时间需要 5 分钟。

LeetCode刷题:925. 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:

name.length <= 1000

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

你的朋友正在使用键盘输入他的名字 name。偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次。

你将会检查键盘输入的字符 typed。如果它对应的可能是你的朋友的名字(其中一些字符可能被长按),那么就返回 True。


算法设计

public boolean isLongPressedName(String name, String typed) {        if(name.length() == 0 && typed.length() == 0){            return true;        }        if(name.length() == 0 || typed.length() == 0){            return false;        }        int n = 0;        for(int i = 0; i < typed.length();i++){            if(typed.charAt(i) == name.charAt(n)){                n++;            }            if(n == name.length()){                return true;            }        }        return false;}

 

转载地址:http://kntdi.baihongyu.com/

你可能感兴趣的文章
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>