Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Subsequence #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions pullrequests/is_subsequence/step1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//lint:file-ignore U1000 Ignore all unused code
package issubsequence

/*
時間:10分
思っていたよりも時間がかかってしまった。
*/
func isSubsequenceStep1(s string, t string) bool {
current := 0
for i := 0; i < len(s); i++ {
for {
Copy link

Choose a reason for hiding this comment

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

https://docs.python.org/3/library/stdtypes.html#str.find
Python の .find(sub, start) 相当のものがあるといいですが、なさそうですね。
https://pkg.go.dev/strings#Index
というように時々標準ライブラリーを調べておくといいです。

if current >= len(t) {
return false
}
if s[i] == t[current] {
current++
break
}
current++
}
}
return true
}
21 changes: 21 additions & 0 deletions pullrequests/is_subsequence/step2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//lint:file-ignore U1000 Ignore all unused code
package issubsequence

/*
よりシンプルにした。
*/
func isSubsequenceStep2(s string, t string) bool {
if len(s) == 0 {
return true
}
current := 0
for i := 0; i < len(t); i++ {
if s[current] == t[i] {
Copy link

Choose a reason for hiding this comment

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

currentだとあんまり情報量がない気もするので、こっちもsi, ti(Goではこういう感じで省略するんでしたっけ?)でいいかもです

current++
}
if current == len(s) {
return true
}
}
return false
}
16 changes: 16 additions & 0 deletions pullrequests/is_subsequence/step3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//lint:file-ignore U1000 Ignore all unused code
package issubsequence

/*
おそらくこれが一番シンプルなのではないだろうか。
*/
func isSubsequenceStep3(s string, t string) bool {
i, j := 0, 0

Choose a reason for hiding this comment

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

for文の外でも使うならi, jの命名をしてもいいと思いました。
s, tのどちらに対応しているくらいが分かると嬉しいです。
s_index, t_indexとかでしょうか。

for i < len(s) && j < len(t) {

Choose a reason for hiding this comment

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

i < len(s) && j < len(t)と条件を並べられてしまうと、jを進めて同じ文字のときにiを進めるという意味が読み取りづらいかと思いました。
step2のコードが意図を把握しやすかったです。

if s[i] == t[j] {
i++
}
j++
}
return i == len(s)
}