From a94cb7c44929fe0b1056090605ad19833f1e15ef Mon Sep 17 00:00:00 2001 From: eaball35 Date: Mon, 16 Sep 2019 07:27:00 -0700 Subject: [PATCH 1/2] finished longest_prefix --- lib/practice_exercises.rb | 49 ++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..82da9f7 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,46 @@ - -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n^2) +# Space Complexity: O(1) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + list.each_with_index do |value, index| + if index > 0 + if value == list[index - 1] + list.slice!(index) + end + end + end + return list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(logn) +# Space Complexity: O(n) + def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" -end + min_string = strings.min { |a,b| a.length <=> b.length} + hi = min_string.length - 1 + low = 0 + mid = (hi + low)/2 + commons = [] + + strings.each do |string| + if min_string == string.slice(low..hi) + commons << string + end + end + return min_string if commons.length == strings.length + + until hi <= low + commons = [] + hi -= 1 + prefix = min_string.slice(low..hi) + + strings.each do |string| + if prefix == string.slice(low..hi) + commons << string + end + end + return prefix if commons.length == strings.length + end + + return "" +end \ No newline at end of file From 81aaae7ffc4b38d74a5c71f319a260316af6554b Mon Sep 17 00:00:00 2001 From: Emily Ball Date: Mon, 16 Sep 2019 22:15:11 -0700 Subject: [PATCH 2/2] Update practice_exercises.rb --- lib/practice_exercises.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 82da9f7..81cf80d 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -11,7 +11,7 @@ def remove_duplicates(list) return list end -# Time Complexity: O(logn) +# Time Complexity: O(n) # Space Complexity: O(n) def longest_prefix(strings) @@ -43,4 +43,4 @@ def longest_prefix(strings) end return "" -end \ No newline at end of file +end