From a229253cdc7ff18d8d51bb9bc2ea8fcc384365b0 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Mon, 16 Sep 2019 08:44:24 -0700 Subject: [PATCH 1/2] lol i forgot to git push it --- lib/practice_exercises.rb | 86 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 6 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..802f10e 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,87 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + # list is a sorted array of numbers + unique_list = [] + + list.each do |element| + unless unique_list[-1] == element + unique_list << element + end + end + + return unique_list end -# Time Complexity: ? -# Space Complexity: ? + + + +# Time Complexity: O(n^2) +# Space Complexity: O(n) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + shortest_string = strings.min_by { |string| string.length} # O(n) + + match = "" + shortest_string.length.times do |index| #O(n) + curr_char = strings[0][index] + + strings.each do |string| #O(n) + if string[index] != curr_char + return match + end + end + + # all strings have matching letter on this index position + match << curr_char + end + + return match +end + + + + + +def longest_prefix_ALT(strings) + # find the longest common prefix string amongst an array of strings + # Time O(n^2) and Space O(n) + + # get the match results from the first 2 strings + prev_match = get_head_matches(strings[0], strings[1]) + + # if the first 2 strings don't have any chars in common, answer is automatically "" + if prev_match == "" + return "" + end + + # compare the prev_match with each subsequent string, modifying prev_match with each iteration + ((strings.length)-2).times do |index| + prev_match = get_head_matches(prev_match, strings[index+2]) + puts + end + + return prev_match end +def get_head_matches(string1, string2) + # O(n) time & O(1) space + shorter_string = [string1, string2].min_by { |str| str.length } + + index = 0 + match = "" + + while index < shorter_string.length + if string1[index] == string2[index] + match << string1[index] + else + break + end + + index += 1 + end + + return match +end + + From 26a1d968e00b25563f1e17c77fd75c8b627b5234 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Mon, 16 Sep 2019 08:46:42 -0700 Subject: [PATCH 2/2] commented out unnecessary stuff --- lib/practice_exercises.rb | 80 ++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 39 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 802f10e..eda0483 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -43,45 +43,47 @@ def longest_prefix(strings) -def longest_prefix_ALT(strings) - # find the longest common prefix string amongst an array of strings - # Time O(n^2) and Space O(n) - - # get the match results from the first 2 strings - prev_match = get_head_matches(strings[0], strings[1]) - - # if the first 2 strings don't have any chars in common, answer is automatically "" - if prev_match == "" - return "" - end - - # compare the prev_match with each subsequent string, modifying prev_match with each iteration - ((strings.length)-2).times do |index| - prev_match = get_head_matches(prev_match, strings[index+2]) - puts - end - - return prev_match -end +#### ALTERNATE METHOD, THOUGHT I COULD DO BETTER THAN O(n^2) BUT COULDN'T #### -def get_head_matches(string1, string2) - # O(n) time & O(1) space - shorter_string = [string1, string2].min_by { |str| str.length } - - index = 0 - match = "" - - while index < shorter_string.length - if string1[index] == string2[index] - match << string1[index] - else - break - end - - index += 1 - end - - return match -end +# def longest_prefix_ALT(strings) +# # find the longest common prefix string amongst an array of strings +# # Time O(n^2) and Space O(n) + +# # get the match results from the first 2 strings +# prev_match = get_head_matches(strings[0], strings[1]) + +# # if the first 2 strings don't have any chars in common, answer is automatically "" +# if prev_match == "" +# return "" +# end + +# # compare the prev_match with each subsequent string, modifying prev_match with each iteration +# ((strings.length)-2).times do |index| +# prev_match = get_head_matches(prev_match, strings[index+2]) +# puts +# end + +# return prev_match +# end + +# def get_head_matches(string1, string2) +# # O(n) time & O(1) space +# shorter_string = [string1, string2].min_by { |str| str.length } + +# index = 0 +# match = "" + +# while index < shorter_string.length +# if string1[index] == string2[index] +# match << string1[index] +# else +# break +# end + +# index += 1 +# end + +# return match +# end