From f22311cdfed5d7e271c808f32f931f5fa5171d69 Mon Sep 17 00:00:00 2001 From: rihib Date: Fri, 16 Aug 2024 00:06:25 +0900 Subject: [PATCH] pullrequests/is_subsequence --- pullrequests/is_subsequence/step1.go | 23 +++++++++++++++++++++++ pullrequests/is_subsequence/step2.go | 21 +++++++++++++++++++++ pullrequests/is_subsequence/step3.go | 16 ++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 pullrequests/is_subsequence/step1.go create mode 100644 pullrequests/is_subsequence/step2.go create mode 100644 pullrequests/is_subsequence/step3.go diff --git a/pullrequests/is_subsequence/step1.go b/pullrequests/is_subsequence/step1.go new file mode 100644 index 0000000..1f76dc4 --- /dev/null +++ b/pullrequests/is_subsequence/step1.go @@ -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 { + if current >= len(t) { + return false + } + if s[i] == t[current] { + current++ + break + } + current++ + } + } + return true +} diff --git a/pullrequests/is_subsequence/step2.go b/pullrequests/is_subsequence/step2.go new file mode 100644 index 0000000..2dd1019 --- /dev/null +++ b/pullrequests/is_subsequence/step2.go @@ -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] { + current++ + } + if current == len(s) { + return true + } + } + return false +} diff --git a/pullrequests/is_subsequence/step3.go b/pullrequests/is_subsequence/step3.go new file mode 100644 index 0000000..5e12304 --- /dev/null +++ b/pullrequests/is_subsequence/step3.go @@ -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 + for i < len(s) && j < len(t) { + if s[i] == t[j] { + i++ + } + j++ + } + return i == len(s) +}