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

Q1_05_One_Away.rb #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 16 additions & 30 deletions Chap_1_Arrays_and_Strings/Q1_05_One_Away.rb
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
def one_away(s1, s2)
if s1 == s2
return true
elsif (s1.length - s2.length).abs > 1
return false
end
s1_array = s1.split('')
s2_array = s2.split('')

string_1 = s1.length >= s2.length ? s1 : s2
string_2 = s1.length < s2.length ? s1 : s2
intersection_length = (s1_array & s2_array).length

chars_1 = string_1.split('')
chars_2 = string_2.split('')

index_1 = 0
index_2 = 0
edit_count = 0

while (index_1 < string_1.length)
if chars_1[index_1] != chars_2[index_2]
edit_count += 1
index_2 += 1 if string_1.length == string_2.length
else
index_2 += 1
end

index_1 += 1

if edit_count > 1
return false
end
end

true
if s1.length >= s2.length
if intersection_length == (s1.length - 1)
true
else
false
end
elsif s1.length < s2.length
if intersection_length == (s2.length - 1)
true
else
false
end
end
end