From 4502644eb4388f681518b1924f09fb535a871ecb Mon Sep 17 00:00:00 2001 From: Kristy Hisaw Date: Mon, 16 Sep 2019 14:22:28 -0700 Subject: [PATCH] updated time and space complexity and passed test --- lib/practice_exercises.rb | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..3be46c7 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,38 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + # go through each element in the array and remove duplicates + + if list.length == 1 || list[0] == nil + return list + end + + list.each_with_index do |item, i| + if item == list[i + 1] + list[i + 1] = nil + end + end + + + return list + end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n * m) +# Space Complexity: O(1) def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + + longest_prefix = "" + strings[0].length.times do |i| + char = strings[0][i] + strings.each_with_index do |string| + if string[i] != char || string[i] == nil + break + elsif char == string[i] && string == strings.last + longest_prefix += char + end + end + end + return longest_prefix end -