Skip to content

Commit

Permalink
feat: add leetcode 392
Browse files Browse the repository at this point in the history
  • Loading branch information
Summer-andy committed May 6, 2022
1 parent 2507913 commit 0a91fbf
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/views/算法篇/判断子序列.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: 判断子序列
date: 2022-05-06
tags:
- leetcode刷题
categories:
- 前端基础
---

## 题目

给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)

输入:s = "abc", t = "ahbgdc"
输出:true


## 题解

首先根据题目意思,s的长度肯定是比t的长度要都都短的,因此我们可以通过双指针的方式,只要s对应的指针标记等于s的长度即表示匹配成功。

```js
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isSubsequence = function(s, t) {
let i = 0, j = 0;

while (i < s.length && j < t.length) {
if(s[i] === t[j]) {
i+= 1;
}
j+= 1;
}

if(i === s.length) return true;

return false;
};
```

0 comments on commit 0a91fbf

Please sign in to comment.