Created
February 5, 2024 05:55
-
-
Save airled/d53e33b08afa1b782e6b260b9dfeed98 to your computer and use it in GitHub Desktop.
Check substring in string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def subs(str1, str2) | |
return nil if str2.size > str1.size | |
str1.each_char.with_index do |char, index| | |
next if char != str2[0] | |
match = true | |
str2[1..].each_char.with_index do |_, str2_slice_index| | |
str2_index = str2_slice_index + 1 | |
next if str2[str2_index] == str1[index + str2_index] | |
match = false | |
break | |
end | |
return index if match | |
end | |
nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment