Skip to content

Instantly share code, notes, and snippets.

@sharmaabhinav
Created September 9, 2014 16:43
Show Gist options
  • Save sharmaabhinav/f021c1fc2d4f6e2995a1 to your computer and use it in GitHub Desktop.
Save sharmaabhinav/f021c1fc2d4f6e2995a1 to your computer and use it in GitHub Desktop.
class Solution
attr_accessor :string1, :string2, :string3
def shortest_length
char_count_arr = create_char_count_array
return 0 if string2.length > string1.length
len1 = string1.length
len2 = string2.length
while len2 <= len1
prev_index = 0
next_index = 0
while next_index < len2
char_count_arr[count_array_index(string1[next_index])] += 1
return len2 if check_array(char_count_arr) == true
next_index += 1
end
while next_index < len1
char_count_arr[count_array_index(string1[next_index])] += 1
char_count_arr[count_array_index(string1[prev_index])] -= 1
return len2 if check_array(char_count_arr)
next_index += 1
prev_index += 1
end
char_count_arr = create_char_count_array
len2 += 1
end
return 0
end
def count_array_index(str)
return str.bytes.first - 97
end
def check_array(char_count_arr)
s2 = true
s3 = true
string2.split("").each do |char|
s2 = false if char_count_arr[count_array_index(char)] == 0
end
string3.split("").each do |char|
s3 = false if char_count_arr[count_array_index(char)] != 0
end
return s2 && s3
end
def create_char_count_array
char_count_arr = []
0.upto(25) do
char_count_arr << 0
end
return char_count_arr
end
end
obj = Solution.new
obj.string1 = "dyittvbiaxyavvvyia"
obj.string2 = "itya"
obj.string3 = "z"
puts obj.shortest_length
obj.string3 = "x"
puts obj.shortest_length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment