判定是否长按

var isLongPressedName = function (name, typed) {
var i = 1, j = 0, n = name.length, m = typed.length;
var last = name[0], iCount = 1
while (i < n || j < m) {
var el = name[i];
if (el !== last) {
if (iCount !== 0) {
let jCount = 0
// console.log("j", j, m)
while (j < m) {
console.log("内循环", last, typed[j], j)
if (typed[j] !== last) {
break //跳到外循环
}
j++
jCount++
} if (jCount < iCount) {
return false
}
if (j == m && i < n) {
return false
}
}
last = el
iCount = 1
} else {
console.log("累加", el)
iCount++
}
i++
}
return true }; console.log(isLongPressedName("alex", "aaleex"))
console.log(isLongPressedName("saeed", "ssaaedd"))
console.log(isLongPressedName("pyplrz", "ppyypllr"))

更精简的实现

var isLongPressedName = function(name, typed) {
let j = 0; for (let i = 0; i < typed.length; i++) {
if(name[j] == typed[i]) j++;
} return j == name.length;
};

另一个

var isLongPressedName = function (name, typed) {
let nlen = name.length, tlen = typed.length;
if (nlen > tlen) return false; let i = 0, j = 0;
while (i < nlen && j < tlen) {
let nc = name.charAt(i);
let tc = typed.charAt(j);
if (nc == tc) {
i++;
j++;
} else {
if (j == 0 || tc != typed.charAt(j - 1)) {
return false;
}
j++;
}
} return i == nlen; };

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

  1. [LeetCode] 925. Long Pressed Name 长按键入的名字

    Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might ...

  2. 【Leetcode_easy】925. Long Pressed Name

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

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

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

  4. [LeetCode&Python] Problem 925. Long Pressed Name

    Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key might ...

  5. 【leetcode】925.Long Pressed Name

    题目如下: Your friend is typing his name into a keyboard.  Sometimes, when typing a character c, the key ...

  6. 925. Long Pressed Name

    题目链接:https://leetcode.com/problems/long-pressed-name/description/ Example 1: Input: name = "ale ...

  7. leetcode 925. 长按键入

    题目描述: 你的朋友正在使用键盘输入他的名字 name.偶尔,在键入字符 c 时,按键可能会被长按,而字符可能被输入 1 次或多次. 你将会检查键盘输入的字符 typed.如果它对应的可能是你的朋友的 ...

  8. 算法与数据结构基础 - 双指针(Two Pointers)

    双指针基础 双指针(Two Pointers)是面对数组.链表结构的一种处理技巧.这里“指针”是泛指,不但包括通常意义上的指针,还包括索引.迭代器等可用于遍历的游标. 同方向指针 设定两个指针.从头往 ...

  9. LeetCode.925-长按的名字(Long Pressed Name)

    这是悦乐书的第355次更新,第380篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第217题(顺位题号是925).你的朋友正在键盘上输入他的名字. 有时,在键入字符c时, ...

随机推荐

  1. c++精度

  2. swiper快速切换插件(两个综合案例源码)

    swiper快速切换插件 swiper.js自己去官网下载哈.先来一个tab切换案例: demo.html <!doctype html> <html> <head> ...

  3. aspose插入图片

    当使用以下代码插入图片时 int iIndex = sheet.Pictures.Add(x, y, PicturePath); Aspose.Cells.Drawing.Picture pic = ...

  4. 神经网络反向传播算法&&卷积神经网络

    听一遍课程之后,我并不太明白这个算法的奇妙之处?? 为啥? 神经网络反向传播算法 神经网络的训练依靠反向传播算法,最开始输入层输入特征向量,网络层计算获得输出,输出层发现输出和正确的类号不一样,这时就 ...

  5. Laravel框架中通过EasyWeChat发送公众号模板消息

    环境要求 PHP >= 7.0 PHP cURL 扩展 PHP OpenSSL 扩展 PHP SimpleXML 扩展 PHP fileinfo 拓展 使用composer安装: $ compo ...

  6. 跳跃【BFS】

    From 牛客网:https://ac.nowcoder.com/acm/problem/25160

  7. springboot~gradle4.7之后的lombok引用方法

    在gradle4.7以后对于加入依赖lombok方式发生变化,gradle4.7版本以前,可以直接如下引用: compile("org.projectlombok:lombok:1.18.2 ...

  8. react-native构建基本页面4---渲染电影列表

    电影列表 import React, { Component } from 'react' import { View, Image, Text, ActivityIndicator, FlatLis ...

  9. js -- 移动端pc端自动切换

    1. 判断浏览器类型 浏览器判断使用的github开源项目current-device,下面是地址: https://github.com/matthewhudson/current-device 在 ...

  10. linux常见目录介绍

    /bin:/usr/bin: 可执行二进制文件目录,如常用命令ls.cat /boot: 放置linux启动时用到的一些文件,建议分区的时候独立分区 /dev: 存在linux系统下的设备文件,访问该 ...