From b292f38714c4fc611d868ee4e780cbfd95e32b08 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 12 Jun 2026 15:53:28 +0000 Subject: [PATCH 1/2] docs: add memo.md template for new branch --- memo.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 memo.md diff --git a/memo.md b/memo.md new file mode 100644 index 0000000..4aa1f71 --- /dev/null +++ b/memo.md @@ -0,0 +1,12 @@ +## Step 1 +```python + +``` +## Step 2 +```python + +``` +## Step 3 +```python + +``` \ No newline at end of file From f9a4de0bf1cb58588aa1f41237b982c71cd36f1e Mon Sep 17 00:00:00 2001 From: fumi1326 Date: Tue, 16 Jun 2026 20:49:29 +0900 Subject: [PATCH 2/2] 392. Is Subsequence --- memo.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/memo.md b/memo.md index 4aa1f71..5196152 100644 --- a/memo.md +++ b/memo.md @@ -1,12 +1,62 @@ +## 今回解いた問題と次に解く問題 +- 今回解いた問題:392. Is Subsequence https://leetcode.com/problems/is-subsequence?envType=problem-list-v2&envId=xo2bgr0r +- 次に解く問題:141. Linked List Cycle https://leetcode.com/problems/linked-list-cycle?envType=problem-list-v2&envId=xo2bgr0r + ## Step 1 -```python +### 方針 +部分文字列sのどの文字をチェックしているか確かめる変数indexを用意して、チェックされる対象のtの前の数字からs[index]と同じかどうかを確かめていく。同じだった場合、indexを1つ後ろにずらして続ける。sの一番後ろの文字について、同じものが見つかったらその時点でTrueを返す。見つからなかったらFalseを返す。時間計算量はO(n)、空間計算量はO(1)。 +### 考えたこと +- sが""だったときはTrueを返すべきなのに、s[0]でアクセスできずエラーになるのでsの長さが0の場合はTrueを返すような処理を加えた。""が入力される場合をあらかじめ考慮すべきであった。 +- indexは変数の名前として適切ではない気がしたが、ほかに何か適切な名前が思い浮かばなかった。 + +```python +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + index = 0 + if len(s) == 0: + return True + for i in range(len(t)): + if s[index] == t[i]: + index += 1 + if index == len(s): + return True + return False ``` ## Step 2 -```python +### 読んだコード +- https://github.com/tom4649/Coding/pull/52/changes +- https://github.com/dxxsxsxkx/leetcode/pull/57/changes +### 考えたこと +- indexはs_indexやt_indexなどと書き換えることができる。 +- sが空な判定に加えて、tが空かどうかの判定も加えたほうがいい。 +- 正規表現で書くやり方もある。この場合、reというモジュールを使う(https://docs.python.org/ja/3/library/re.html)。matchはstringの前方の正規表現がpatternと一致した場合にMatchオブジェクトを返し、一致しなかった場合はNoneを返す関数。文字列の結合の時間計算量はO(n^2)だが、sが小さいので許容範囲か。 +```python +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + if len(s) == 0: + return True + if len(t) == 0: + return False + s_index = 0 + for i in range(len(t)): + if s[s_index] == t[i]: + s_index += 1 + if s_index == len(s): + return True + return False ``` -## Step 3 +## Step 3(正規表現で書く) + ```python +import re +class Solution: + def isSubsequence(self, s: str, t: str) -> bool: + pattern = "" + for c in s: + pattern += ".*" + c + Match = re.match(pattern, t) + return Match is not None ``` \ No newline at end of file