Skip to content

Instantly share code, notes, and snippets.

@truongnmt
Last active February 13, 2020 15:41
Show Gist options
  • Save truongnmt/1d795fd19c6d7b7f4702c79b9e59c6d0 to your computer and use it in GitHub Desktop.
Save truongnmt/1d795fd19c6d7b7f4702c79b9e59c6d0 to your computer and use it in GitHub Desktop.
# @param {String} s
# @param {String} t
# @return {Boolean}
# def is_subsequence(s, t)
# i = 0
# t.each_char do |c|
# i += 1 if c == s[i]
# end
# i == s.length
# end
def is_subsequence(s, t)
return s == t if s.length == t.length
j = 0
last_found_index = 0
found = 0
loop do
for x in last_found_index..((t.length-last_found_index)/2+last_found_index) do
if s[j] == t[x]
found = 1
j+=1
last_found_index = x+1
break
elsif last_found_index == (t.length-1)
last_found_index = x+1
found = 0
else
found = 0
end
end
if found == 0
last_found_index = (t.length-last_found_index)/2+last_found_index
end
break if j == s.length
break if last_found_index >= t.length
end
j == s.length
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment