From b7fe412166221d41e6b56faa0e64115d975e51de Mon Sep 17 00:00:00 2001 From: "T.Sadamori" Date: Mon, 20 Jul 2026 16:52:03 +0900 Subject: [PATCH 1/2] step1 --- 0392-is-subsequence/memo.md | 36 ++++++++++++++++++++++++++++++++++++ 0392-is-subsequence/step1.py | 26 ++++++++++++++++++++++++++ 0392-is-subsequence/step2.py | 0 3 files changed, 62 insertions(+) create mode 100644 0392-is-subsequence/memo.md create mode 100644 0392-is-subsequence/step1.py create mode 100644 0392-is-subsequence/step2.py diff --git a/0392-is-subsequence/memo.md b/0392-is-subsequence/memo.md new file mode 100644 index 0000000..2b04784 --- /dev/null +++ b/0392-is-subsequence/memo.md @@ -0,0 +1,36 @@ +# 392. Is Subsequence + +## Step1 +- ふたつのstr、sとtが与えられて、tのなかにsの各文字が順番通り入っていればtrueを返す。 +- sをインデックスi、tを走査して、 + - s[i] == t[j]なら両方進める + - != ならtだけ進める +- tを走査し終わったとき(len(t) == j)、sも最後まで走査が進んでいれば(len(s) == i)、OK。 + +- 二重ループではなく、単にふたつのループが並走するので時間計算量はO(n)。 +- 条件`0 <= t.length <= 10^4`を満たすには十分。 +- 空間計算量は、インデックスの保持だけなので、O(1)。 + +```python +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + i = 0 + j = 0 + + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + + if i == len(s): + return True + else: + return False +``` + + +# Step2 +- returnは`return i==len(s)`でよさそう。 +- よく言及されているsやtの長さが0のときの早期リターンは、明示的ではあるものの、ループの条件ですぐ弾けるので不要かと思う。 +- `if len(s) > len(t): + return False`(https://github.com/kazuki-official/leetcode/blob/392-is-subsequence/memo.md)はやる価値があるか。ただここで削れるのも高々100ステップなので必須ではないかと思う。 \ No newline at end of file diff --git a/0392-is-subsequence/step1.py b/0392-is-subsequence/step1.py new file mode 100644 index 0000000..54e4b5f --- /dev/null +++ b/0392-is-subsequence/step1.py @@ -0,0 +1,26 @@ +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + i = 0 + j = 0 + + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + + if i == len(s): + return True + else: + return False + + +def main() -> None: + s = "oho" + t = "" + + Solver = Solution() + res = Solver.isSubsequence(s, t) + print(res) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/0392-is-subsequence/step2.py b/0392-is-subsequence/step2.py new file mode 100644 index 0000000..e69de29 From 025984796f9e31451e147222c057cc8091fdfd62 Mon Sep 17 00:00:00 2001 From: "T.Sadamori" Date: Tue, 21 Jul 2026 02:26:21 +0900 Subject: [PATCH 2/2] step2 --- 0392-is-subsequence/memo.md | 37 ++++++++++++++++++++++++++++++++++-- 0392-is-subsequence/step2.py | 11 +++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/0392-is-subsequence/memo.md b/0392-is-subsequence/memo.md index 2b04784..5772ae3 100644 --- a/0392-is-subsequence/memo.md +++ b/0392-is-subsequence/memo.md @@ -29,8 +29,41 @@ class Solution: ``` -# Step2 +## Step2 - returnは`return i==len(s)`でよさそう。 - よく言及されているsやtの長さが0のときの早期リターンは、明示的ではあるものの、ループの条件ですぐ弾けるので不要かと思う。 - `if len(s) > len(t): - return False`(https://github.com/kazuki-official/leetcode/blob/392-is-subsequence/memo.md)はやる価値があるか。ただここで削れるのも高々100ステップなので必須ではないかと思う。 \ No newline at end of file + return False` + (https://github.com/kazuki-official/leetcode/blob/392-is-subsequence/memo.md)はやる価値があるか。ただここで削れるのも高々100ステップなので必須とはいえないかと思う。 +- `s_index`, `t_index`という変数名も多かったが、`i`, `j`もループがふたつのときにおけるインデックスとしてメジャーだと考える。 +- 正規表現でも書ける(https://github.com/lightbanana/leetcode/pull/3/changes)。やってることがわかりやすいので割と好き。 + ```python + import re + + class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + pattern = "" + for c in s: + pattern += ".*" + c #任意の連続しうる文字をsの構成文字の間に挟む + + Match = re.match(pattern, t) + return True if Match else False + ``` + - ただnodchipさんがコメントしている通り、Pythonでは文字列の+=で再構築が走るので避けたほうがよい。 +- 変更点はreturn文くらいとした。 +```python +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + i = 0 + j = 0 + + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + + return i == len(s) +``` + +## Step3 +- 上記コードを三回再現。 \ No newline at end of file diff --git a/0392-is-subsequence/step2.py b/0392-is-subsequence/step2.py index e69de29..e2a929f 100644 --- a/0392-is-subsequence/step2.py +++ b/0392-is-subsequence/step2.py @@ -0,0 +1,11 @@ +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + i = 0 + j = 0 + + while i < len(s) and j < len(t): + if s[i] == t[j]: + i += 1 + j += 1 + + return i == len(s) \ No newline at end of file