判定是否长按

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. [开发技巧]·AttributeError: module 'pywt' has no attribute 'wavedec'解决方法

    [开发技巧]·AttributeError: module 'pywt' has no attribute 'wavedec'解决方法 1.卸载 pywt pip uninstall pywt 2.安 ...

  2. 【笔记】机器学习 - 李宏毅 - 3 - Bias & Variance

    A more complex model does not always lead to better performance on testing data. Because error due t ...

  3. Wannafly Winter Camp 2020 Day 7D 方阵的行列式 - 数学

    于是去弄了个板子来 #include <bits/stdc++.h> using namespace std; #define int long long const int mod = ...

  4. 小程序texarea 输入内容回显失败

    原因:输入框是textarea,输入的数据是含有换行符的字符串,小程序能渲染这种数据的标签有text,和textarea.(view 标签不能识别 /n 等字符) 1.使用text失败是由于不能覆盖视 ...

  5. npm常用模块汇总

    npm常用模块汇总: 点击插件名字,查看使用文档 npm常用模块汇总 node常用模块汇总 gulp常用插件汇总 npx 使用教程:npx使用教程 bable:bable这是JavaScript编译器 ...

  6. leetcode四道组合总和问题总结(39+40+216+377)

    39题目: 链接:https://leetcode-cn.com/problems/combination-sum/ 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 ...

  7. Milking Time POJ - 3616 dp 感觉像背包

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> us ...

  8. 洛谷P1308 统计单词数

    原题链接:https://www.luogu.org/problem/P1308 #include<iostream> #include<cstring> #include&l ...

  9. 连接数据库报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    报错: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.soc ...

  10. Selenium3+python自动化012+日志logging基本用法、高级用法

    1.关键字: login 登录 log 日志 logging python日志模块 2.什么叫日志: 日志用来记录用户行为或者代码的执行过程 3.日志使用的地方: 1.排错的时候需要打印很多细节来帮助 ...