Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions 392. Is Subsequence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 進め方

Step1 : 問題を解く。

Step2 : 他の人のPRを参照し、コメントする。

Step3 : 3回続けてエラーが出ないように書く。ドキュメントを参照する。

# 実践

## Step1

### 思考ログ

```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
if not s:
return True
s_index = 0
for char in t:
if char != s[s_index]:
continue
s_index += 1
if s_index == len(s):
return True
return False
```

sが空のケースを忘れた。
O(N)

## Step2

### 同じ問題を解いた人のプルリクを見る

https://github.com/olsen-blue/Arai60/pull/58/files

> char は C/C++ で予約語のため、チーム内に C++ を書く方がいる場合は避けたほうが無難だと思います。 c または ch が良いかもしれません。
>
- その視点はあまりありませんでした。

> 何がいいたかったかというと、このあたりの変形を通して、他のやつと同じになります。
>

> 変形の仕方も頭において、どう書くと見通しがよく伝わるかを考えてみましょう。
>
- 変形の仕方は考えたほうがよさそう。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

局所的な変形で見通しを良くするというのはよくやることかと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

振り返ってみると三回書くフェーズで自然に思いつく気がします。


https://github.com/hroc135/leetcode/pull/52

- 二分探索でも解けるみたい。

https://github.com/usatie/leetcode/pull/5/files

- 副作用について気にしないのは不安に感じました。
- あえて分かりにくい解法で解くの私はあまり好きではないですね。

```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
assert s is not None
s_index = 0
t_index = 0
while s_index < len(s):
if t_index >= len(t):
return False
if s[s_index] == t[t_index]:
s_index += 1
t_index += 1
return True
```

このwhileが一番シンプルな感じがする。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、見やすいと思います。


## Step3

### 3回連続で再現

```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
s_i = 0
t_i = 0
while s_i < len(s):
if t_i >= len(t):
return False
if s[s_i] == t[t_i]:
s_i += 1
t_i += 1
return True
```

indexをiとしたが許容される気がする。