From 643961fbdf22493a03e7d53771983816d62ce521 Mon Sep 17 00:00:00 2001 From: emaust Date: Mon, 16 Sep 2019 18:45:30 -0700 Subject: [PATCH 1/2] Remove duplicates completed --- lib/practice_exercises.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..29f23c1 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,29 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + index = 0 + while index <= list.length + if list[index] == list[index + 1] + list.delete_at(index) + end + index += 1 + end + return list end -# Time Complexity: ? + + + # Space Complexity: ? -def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" -end +# def longest_prefix(strings) +# first_word = strings[0] +# length = strings.length +# length -= length + +# length.times do |index| +# comparison_word = array[index + 1] + +# end + From 60e594da5aa726058f4478881c15e47e939e72ea Mon Sep 17 00:00:00 2001 From: emaust Date: Mon, 16 Sep 2019 20:10:56 -0700 Subject: [PATCH 2/2] Implemented longest_prefix --- lib/practice_exercises.rb | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 29f23c1..7aa213a 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,4 +1,4 @@ - +require 'pry' # Time Complexity: O(n) # Space Complexity: O(n) def remove_duplicates(list) @@ -14,16 +14,32 @@ def remove_duplicates(list) +# Time Complexity: O(n * m) +# Space Complexity: O(1) + +def longest_prefix(strings) + prefix = strings[0] + original = prefix + a_length = strings.length + a_length -= 1 + + + a_length.times do |index| + comparison_word = strings[index + 1] + w_length = prefix.length + w_length.times do |i| + if prefix[i] != comparison_word[i] + i -= 1 + prefix = prefix[0..i] + if prefix == original + return "" + end + break + end + end + end + return prefix +end -# Space Complexity: ? -# def longest_prefix(strings) -# first_word = strings[0] -# length = strings.length -# length -= length - -# length.times do |index| -# comparison_word = array[index + 1] - -# end