-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2507913
commit 0a91fbf
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
``` |