Leetcode之动态规划(DP)专题-392. 判断子序列(Is Subsequence)


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

你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。

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

示例 1:
s = "abc", t = "ahbgdc"

返回 true.

示例 2:
s = "axc", t = "ahbgdc"

返回 false.


DP定义:

dp[i][j]表示长度为i的字符串s是否为长度为j的字符串t的子序列。

状态转移方程:

if(s.charAt(i)==t.charAt(j)){

  dp[i][j] = dp[i-1][j-1];

}else{

  dp[i][j] = dp[i][j-1];

}

解释,以示例1为例:

首先,当s为空串的时候,s始终为t的子序列。

然后,如果s.charAt(i)==t.charAt(j),长度为i的s是否是长度为j的t的子串取决于长度为i-1的s是否是长度为j-1的t的子串。

如果不想等,那么把t缩短1个单位长度,看看是否为true

例如,当s="ab" t="ahbg"时,'b'≠'g',那么把t缩短1个单位长度,变成t="ahb"看看是否为true,如果为true,那么增加1个单位长度后也一定为true

class Solution {
public boolean isSubsequence(String s, String t) {
int len1 = s.length();
int len2 = t.length();
boolean[][] dp = new boolean[len1+1][len2+1];
for (int i = 0; i < dp[0].length; i++) {
dp[0][i] = true;
}
for (int i = 1; i < len1+1; i++) {
for (int j = 1; j < len2+1; 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[len1][len2];
}
}

Leetcode之动态规划(DP)专题-392. 判断子序列(Is Subsequence)的更多相关文章

  1. [Leetcode 392]判断子序列 Is Subsequence

    [思路] 判断s是否为t的子串,所以length(s)<=length(t).于是两个指针,一次循环. 将s.t转换为数组p1.p2. i为过程中s的匹配长度. i=0空串,单独讨论返回true ...

  2. Java实现 LeetCode 392 判断子序列

    392. 判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符 ...

  3. Leetcode 392.判断子序列

    判断子序列 给定字符串 s 和 t ,判断 s 是否为 t 的子序列. 你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 ...

  4. [LeetCode] 392. 判断子序列 ☆(动态规划)

    https://leetcode-cn.com/problems/is-subsequence/solution/java-dp-by-zxy0917-5/ 描述 给定字符串 s 和 t ,判断 s ...

  5. 动态规划dp专题练习

    貌似开坑还挺好玩的...开一个来玩玩=v=... 正好自己dp不是很熟悉,就开个坑来练练吧...先练个50题?小目标... 好像有点多啊QAQ 既然是开坑,之前写的都不要了! 50/50 1.洛谷P3 ...

  6. hdu 1503:Advanced Fruits(动态规划 DP & 最长公共子序列(LCS)问题升级版)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. [Swift]LeetCode392. 判断子序列 | Is Subsequence

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  8. LeetCode:动态规划

    动态规划 动态规划永远的神 这部分主要是学习了 labuladong 公众号中对于动态规划的讲解 刷了些 leetcode 题,在此做一些记录,不然没几天就忘光光了 题目 这部分内容直接上题目了,解题 ...

  9. Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)

    Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...

随机推荐

  1. HDU 6041 - I Curse Myself | 2017 Multi-University Training Contest 1

    和题解大致相同的思路 /* HDU 6041 - I Curse Myself [ 图论,找环,最大k和 ] | 2017 Multi-University Training Contest 1 题意 ...

  2. 【Winfrom-禁止重复启动程序】 程序不能重复启动

    using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Sy ...

  3. vim8.1安装

    win下直接就有gvim8.1.exe安装.但linux下直接从apt-get里面下载的vim都是远古版本,需要手动编译安装. 首先,下载vim源代码 git clone https://github ...

  4. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. How to correctly set application badge value in iOS 8?

    o modify the badge under ios8 you have to ask for permissions let settings = UIUserNotificationSetti ...

  6. react 脚手架 及路由和 redux

    前提是我们需要下载 nodejs 使用 npm 下载 react 的脚手架,react-router-dom,redux 全局下载 react 的脚手架:npm i create-react-app ...

  7. 往Angular应用程序中添加DevExtreme

    To start this tutorial, you need an Angular 5+ application created using the Angular CLI. Refer to t ...

  8. 判断List是否为空的问题

    今天公司安排给页面调试Bug,感觉公司人员写的判断判断List是否为空存在一定的问题,公司判断是list!=null这是完全不对的,这只会判断是否有list对象.如果为空,他也会执行if(list!= ...

  9. leetcode1281 整数的各位积和之差

    class Solution { public: int subtractProductAndSum(int n) { ; ; ){ ; n/=; prod*=r; add+=r; } int res ...

  10. [转]springboot启动原理

    参考文章:https://www.jianshu.com/p/ef6f0c0de38f