Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Two-pointer walk:
let is_subseq s t =
let i = ref 0 in
let j = ref 0 in
while !i < n && !j < m do
if s.[!i] = t.[!j] then i := !i + 1;
j := !j + 1
done;
!i = n
advance i only on match; always advance j. Pattern matches if i
reaches n.
Five test cases:
'abc' in 'ahbgdc' yes
'axc' in 'ahbgdc' no (no x in t)
'' in 'anything' yes (empty trivially)
'abc' in 'abc' yes
'abcd' in 'abc' no (s longer)
sum = 3
93 baseline programs total.
19 lines
445 B
OCaml
19 lines
445 B
OCaml
let is_subseq s t =
|
|
let i = ref 0 in
|
|
let n = String.length s in
|
|
let m = String.length t in
|
|
let j = ref 0 in
|
|
while !i < n && !j < m do
|
|
if s.[!i] = t.[!j] then i := !i + 1;
|
|
j := !j + 1
|
|
done;
|
|
!i = n
|
|
|
|
;;
|
|
|
|
(if is_subseq "abc" "ahbgdc" then 1 else 0) +
|
|
(if is_subseq "axc" "ahbgdc" then 1 else 0) +
|
|
(if is_subseq "" "anything" then 1 else 0) +
|
|
(if is_subseq "abc" "abc" then 1 else 0) +
|
|
(if is_subseq "abcd" "abc" then 1 else 0)
|