判定是否长按

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. NODEJS 中使用二维码 qr-image

    https://www.npmjs.com/package/qr-image npm install qr-image var qr = require('qr-image'); var code = ...

  2. gulp常用插件之gulp-rev-format使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-rev-format这是一款提供静态资产的哈希格式选项(前缀,后缀,最后扩展名). 更多使用文档请点击访问gulp-rev-format ...

  3. git客户端的常用命令

    注意:仓库只有管理员建的你才有权限上传,不然自己建的也没用,没权限上传 1.远程仓库路径查询 git remote -v 2.添加远程仓库 git remote add origin <你的项目 ...

  4. Codeforces Round #616 Coffee Varieties

    题意 不太容易讲清,看英文吧 codeforces 做法 先从简单的看起 将块以\(\frac{k}{2}\)个元素为界,然后类似线段树一样递归下去,每次一层的左子树跟右子树的块相互暴力比较 \[\b ...

  5. 错误 CS0234 命名空间“Microsoft”中不存在类型或命名空间名“Reporting”(是否缺少程序集引用?)

    VS2017 使用Microsoft Rdlc报表设计器出现以下错误: 错误 CS0234 命名空间“Microsoft”中不存在类型或命名空间名“Reporting”(是否缺少程序集引用?) 以及其 ...

  6. H5_0015:判断是否是微信加载

        var e = document.createElement("script");     e.src = "https://res.wx.qq.com/open ...

  7. golang channel 的一次内存错误

    起因 原因调查 原因分析 问题解决 总结 起因 今天在做数据库数据读取时, 首先通过多个 goroutine 将从数据库读取的数据写入 channel, 同时通过另一个 goroutine 从 cha ...

  8. JavaScript仿计算器案例源代码

    效果图 index.html <!DOCTYPE html> <html> <head> <title></title> <link ...

  9. liunx 查找locate

    使用 安装 yum install mlocate 更新数据库 updatedb 查找my.cnf文件 locate my.cnf

  10. Android_Activity的生命周期、跳转方式及参数传递、启动模式。

    Activity的生命周期: onCreat ,onStart,onResume,onPause,onRestart,onStop,onDestroy Activity之间的跳转分为显式跳转和隐式跳转 ...