MelodyHub

没有伞的孩子必须努力奔跑!|

题目描述

给定字符串 s 和 t ,判断 s 是否为 t 的子序列。

你可以认为 s 和 t 中仅包含英文小写字母。

字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。

字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,”ace”是”abcde”的一个子序列,而”aec”不是)。

示例 1:

1
2
3
s = "abc", t = "ahbgdc"

返回 true.

示例 2:

1
2
3
s = "axc", t = "ahbgdc"

返回 false.

后续挑战 :

如果有大量输入的 S,称作S1, S2, … , Sk 其中 k >= 10亿,你需要依次检查它们是否为 T 的子序列。在这种情况下,你会怎样改变代码?

题解

贪心法+双指针

简单的办法就是使用双滑动指针,思路好理解,直接上算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
/* 双指针的贪心算法 */
public boolean isSubsequence(String s, String t) {
int sL = s.length();
int tL = t.length();
int i = 0, j = 0;
//包含了s为空或s>t的情况
while(i < sL && j < tL){
if(s.charAt(i) == t.charAt(j)){
i++;
j++;
} else {
j++;
}
}
return i == sL;
}
}

aiabuQ.png

动态规划

从双指针的贪心算法中,可以推导出下面这两个式子:

1
2
3
4
5
s.charAt(i-1) == t.charAt(j-1)
=> dp[i][j] = dp[i-1][j-1]

s.charAt(i-1) != t.charAt(j-1)
=> dp[i][j] = dp[i][j-1]

dff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
/* 动态规划 */
public boolean isSubsequence(String s, String t) {
if(s.length()==0) return true;
if(s.length() > t.length()) return false;
boolean[][] dp = new boolean[s.length()+1][t.length()+1];
//初始化
for (int j = 0; j < t.length(); j++) {
dp[0][j] = true;
}
int sL = s.length();
int tL = t.length();
//dp
for (int i = 1; i <= sL; i++) {
for (int j = 1; j <= tL; j++) {
if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = dp[i][j - 1];
}
}
}
return dp[sL][tL];
}
}

ai2wE8.png

递归迭代+单指针

  • 对于两个字符串,判断其中一个字符串是否是另外一个字符串的子序列,可以采用循环迭代+单指针的方式实现判断,能够简单快速实现功能。
  1. 以最短字符串为基准,循环迭代,分别获取位置由小到大的字符;
  2. 设置字符串t的指针位置index,用于判断s的字符是否处于字符串t中的指针位置index到字符串尾部;
  3. 如果s的字符处于字符串t的指定字符串范围中,则index加1,不断重复该操作;
  4. 如果s的字符不处于字符串t的指定字符串范围中,则直接返回false;

作者:andy_fu
链接:https://leetcode-cn.com/problems/is-subsequence/solution/xun-huan-die-dai-dan-zhi-zhen-jian-dan-kuai-su-shi/

aihzwt.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
/* 递归迭代+单指针 */
public boolean isSubsequence(String s, String t) {
if(s.length() > t.length()) return false;
int index = 0;
for(int i=0;i<s.length();i++){
// if exists next index positioin
if((index = t.indexOf(String.valueOf(s.charAt(i)),index) + 1) == 0){
return false;
}
}
return true;
}
}
  • String.valueOf

aih3Zt.png

  • String.indexOf

aifygH.png

LCS最长公共子序列

这题是可以用最长公共子序列的模板来做,但是估计时间复杂度会比较大。算法,略。

精选题解

  1. 官方题解
  2. java的4种解法(第三种方式双百)(公共子序列,双指针,逐个查找,动态规划)

我们知道java中String类有这样一个方法public int indexOf(int ch, int fromIndex) ,他表示的是在字符串中是否存在一个字符ch,并且是从字符串的下标fromIndex开始查找的。我们要做的是在t字符串中查找s中的每一个字符,如果没查到,直接返回false。如果查到,就从t的下一个位置继续开始查。

作者:sdwwld
链接:https://leetcode-cn.com/problems/is-subsequence/solution/javade-2chong-jie-fa-by-sdwwld/

1
2
3
4
5
6
7
8
9
10
11
public boolean isSubsequence(String s, String t) {
int index = -1;
for (char c : s.toCharArray()) {
//index表示上一次查找的位置(第一次查找的时候为-1),所以这里要从t的下标(index+1)开始查找
index = t.indexOf(c, index + 1);
//没找到,返回false
if (index == -1)
return false;
}
return true;
}

 评论


博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

本站使用 Hexo 作为驱动引擎 , 总浏览量为 次 , 总访客数为
载入天数...载入时分秒...