|
| 1 | +# 392. Is Subsequence |
| 2 | +LeetCode problem: [392. Is Subsequence](https://leetcode.com/problems/is-subsequence/){:target="_blank"} |
| 3 | + |
| 4 | +## Problem |
| 5 | +> Given two strings `s` and `t`, return `true` if s is a **subsequence** of `t`, or `false` otherwise. |
| 6 | +
|
| 7 | +A **subsequence** of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., `"ace"` is a subsequence of `"abcde"` while `"aec"` is not). |
| 8 | + |
| 9 | +``` |
| 10 | +Example 1: |
| 11 | +Input: s = "abc", t = "ahbgdc" |
| 12 | +Output: true |
| 13 | +
|
| 14 | +Example 2: |
| 15 | +Input: s = "axc", t = "ahbgdc" |
| 16 | +Output: false |
| 17 | +``` |
| 18 | + |
| 19 | +## Thoughts |
| 20 | +* This problem can be solved by using `two pointers`, but here we will use another way. |
| 21 | +* It is a question of comparing two strings. After doing similar questions many times, we will develop an intuition to use dynamic programming with two-dimensional arrays. |
| 22 | + |
| 23 | +### Common steps in dynamic programming |
| 24 | +These five steps are a pattern for solving dynamic programming problems. |
| 25 | + |
| 26 | +1. Determine the meaning of the `dp[i][j]` |
| 27 | + * Since there are two strings, we can use two-dimensional arrays to simplify the process. |
| 28 | + * `dp[i][j]` represents whether the first `i` letters of `s` are a subsequence of `t`'s first j letters. |
| 29 | + * The value of `dp[i][j]` is `true` or `false`. |
| 30 | +2. Determine the `dp` array's recurrence formula |
| 31 | + * Use an example: |
| 32 | +``` |
| 33 | +s = "abc", t = "ahbgdc" |
| 34 | +# a h b g d c |
| 35 | +# T T T T T T T |
| 36 | +# a F T T T T T T |
| 37 | +# b F F F T T T T |
| 38 | +# c F F F F F F T |
| 39 | +``` |
| 40 | + * Recurrence formula: |
| 41 | +``` |
| 42 | +if s[i - 1] == t[j - 1] |
| 43 | + dp[i][j] = dp[i - 1][j - 1] |
| 44 | +else |
| 45 | + dp[i][j] = dp[i][j - 1] |
| 46 | +``` |
| 47 | + |
| 48 | +3. Determine the `dp` array's initial value |
| 49 | + * `dp[0][j] = true` because `dp[0]` represents the empty string and empty string is a subsequence of any string. |
| 50 | + * `dp[i][j] = false (i != 0)`. |
| 51 | +4. Determine the `dp` array's traversal order |
| 52 | + * `dp[i][j]` depends on `dp[i - 1][j - 1]` and `dp[i][j - 1]`, so we should traverse the `dp` array from top to bottom, then from left to right. |
| 53 | +5. Check the `dp` array's value |
| 54 | + * Print the `dp` to see if it is as expected. |
| 55 | + |
| 56 | +### Complexity |
| 57 | +* Time: `O(n * m)`. |
| 58 | +* Space: `O(n * m)`. |
| 59 | + |
| 60 | +## Python |
| 61 | +```python |
| 62 | +class Solution: |
| 63 | + def isSubsequence(self, s: str, t: str) -> bool: |
| 64 | + column_count = len(t) + 1 |
| 65 | + dp = [[True] * column_count] |
| 66 | + for _ in s: |
| 67 | + dp.append([False] * column_count) |
| 68 | + |
| 69 | + for i in range(1, len(dp)): |
| 70 | + for j in range(1, len(dp[0])): |
| 71 | + if s[i - 1] == t[j - 1]: |
| 72 | + dp[i][j] = dp[i - 1][j - 1] |
| 73 | + else: |
| 74 | + dp[i][j] = dp[i][j - 1] |
| 75 | + |
| 76 | + return dp[-1][-1] |
| 77 | +``` |
| 78 | + |
| 79 | +## C++ |
| 80 | +```cpp |
| 81 | +class Solution { |
| 82 | +public: |
| 83 | + bool isSubsequence(string s, string t) { |
| 84 | + vector<vector<bool>> dp(s.size() + 1, vector<bool>(t.size() + 1)); |
| 85 | + fill(dp[0].begin(), dp[0].end(), true); |
| 86 | + |
| 87 | + for (int i = 1; i < dp.size(); i++) { |
| 88 | + for (int j = 1; j < dp[0].size(); j++) { |
| 89 | + if (s[i - 1] == t[j - 1]) |
| 90 | + dp[i][j] = dp[i - 1][j - 1]; |
| 91 | + else |
| 92 | + dp[i][j] = dp[i][j - 1]; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return dp[s.size()][t.size()]; |
| 97 | + } |
| 98 | +}; |
| 99 | +``` |
| 100 | + |
| 101 | +## Java |
| 102 | +```java |
| 103 | +class Solution { |
| 104 | + public boolean isSubsequence(String s, String t) { |
| 105 | + var dp = new boolean[s.length() + 1][t.length() + 1]; |
| 106 | + Arrays.fill(dp[0], true); |
| 107 | + |
| 108 | + for (int i = 1; i < dp.length; i++) { |
| 109 | + for (int j = 1; j < dp[0].length; j++) { |
| 110 | + if (s.charAt(i - 1) == t.charAt(j - 1)) |
| 111 | + dp[i][j] = dp[i - 1][j - 1]; |
| 112 | + else |
| 113 | + dp[i][j] = dp[i][j - 1]; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return dp[s.length()][t.length()]; |
| 118 | + } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +## C# |
| 123 | +```c# |
| 124 | +public class Solution { |
| 125 | + public bool IsSubsequence(string s, string t) { |
| 126 | + var dp = new bool[s.Length + 1][]; |
| 127 | + for (int i = 0; i < dp.Length; i++) { |
| 128 | + dp[i] = new bool[t.Length + 1]; |
| 129 | + } |
| 130 | + Array.Fill(dp[0], true); |
| 131 | + |
| 132 | + for (int i = 1; i < dp.Length; i++) { |
| 133 | + for (int j = 1; j < dp[0].Length; j++) { |
| 134 | + if (s[i - 1] == t[j - 1]) |
| 135 | + dp[i][j] = dp[i - 1][j - 1]; |
| 136 | + else |
| 137 | + dp[i][j] = dp[i][j - 1]; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return dp[s.Length][t.Length]; |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## JavaScript |
| 147 | +```javascript |
| 148 | +var isSubsequence = function(s, t) { |
| 149 | + let dp = Array(s.length + 1).fill().map( |
| 150 | + () => Array(t.length + 1).fill(false) |
| 151 | + ) |
| 152 | + dp[0].fill(true) |
| 153 | + |
| 154 | + for (let i = 1; i < dp.length; i++) { |
| 155 | + for (let j = 1; j < dp[0].length; j++) { |
| 156 | + if (s[i - 1] == t[j - 1]) |
| 157 | + dp[i][j] = dp[i - 1][j - 1] |
| 158 | + else |
| 159 | + dp[i][j] = dp[i][j - 1] |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return dp[dp.length - 1][dp[0].length - 1] |
| 164 | +}; |
| 165 | +``` |
| 166 | + |
| 167 | +## Go |
| 168 | +```go |
| 169 | +func isSubsequence(s string, t string) bool { |
| 170 | + dp := make([][]bool, len(s) + 1) |
| 171 | + column_count := len(t) + 1 |
| 172 | + dp[0] = slices.Repeat([]bool{true}, column_count) |
| 173 | + for i := 1; i < len(dp); i++ { |
| 174 | + dp[i] = make([]bool, column_count) |
| 175 | + } |
| 176 | + |
| 177 | + for i := 1; i < len(dp); i++ { |
| 178 | + for j := 1; j < len(dp[0]); j++ { |
| 179 | + if s[i - 1] == t[j - 1] { |
| 180 | + dp[i][j] = dp[i - 1][j - 1] |
| 181 | + } else { |
| 182 | + dp[i][j] = dp[i][j - 1] |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + return dp[len(dp) - 1][len(dp[0]) - 1] |
| 188 | +} |
| 189 | +``` |
| 190 | + |
| 191 | +## Ruby |
| 192 | +```ruby |
| 193 | +def is_subsequence(s, t) |
| 194 | + dp = Array.new(s.size + 1) do |i| |
| 195 | + Array.new(t.size + 1, i == 0 ? true : false) |
| 196 | + end |
| 197 | + |
| 198 | + for i in 1..dp.size - 1 |
| 199 | + for j in 1..dp[0].size - 1 |
| 200 | + dp[i][j] = |
| 201 | + if s[i - 1] == t[j - 1] |
| 202 | + dp[i - 1][j - 1] |
| 203 | + else |
| 204 | + dp[i][j - 1] |
| 205 | + end |
| 206 | + end |
| 207 | + end |
| 208 | + |
| 209 | + dp[-1][-1] |
| 210 | +end |
| 211 | +``` |
0 commit comments