From 14cebe025d6a59da606767c0c66dd727aa43a5d1 Mon Sep 17 00:00:00 2001 From: Eve Le Date: Sun, 15 Sep 2019 22:20:51 -0700 Subject: [PATCH 1/2] Implement remove_duplicates and longest_prefix --- lib/practice_exercises.rb | 53 ++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..323f922 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,54 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(1) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + raise ArgumentError if !list + length = list.length + + if length > 0 + unique_index = 0 + unique_element = list.first + + length.times do |index| + if list[index] != unique_element + unique_element = list[index] + unique_index += 1 + list[unique_index] = unique_element + end + + if index > unique_index + list[index] = nil + end + end + + end + return list end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n * m) where n is the number of strings in input array, m is the length +# (number of characters) of either prefix string or current string. +# Space Complexity: O(1) because this method uses constant number of temp variables and returns +# only 1 string (it might contain m characters but still only 1 string) + def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + raise ArgumentError if !strings + + prefix = "" + if strings.length > 0 + prefix = strings[0] + (strings.length - 1).times do |i| + current_string = strings[i + 1] + + length = prefix.length < current_string.length ? prefix.length : current_string.length + length.times do |j| + if prefix[j] != current_string[j] + prefix = prefix[0...j] + break + end + end + end + end + + return prefix end From 86bc3bfd12a0f41624467e45c7311abcb5faa44c Mon Sep 17 00:00:00 2001 From: Eve Le Date: Sun, 15 Sep 2019 22:41:24 -0700 Subject: [PATCH 2/2] Add test case and update longest_prefix --- lib/practice_exercises.rb | 8 +++++++- test/practice_exercises_test.rb | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 323f922..bdfefc0 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -39,7 +39,13 @@ def longest_prefix(strings) (strings.length - 1).times do |i| current_string = strings[i + 1] - length = prefix.length < current_string.length ? prefix.length : current_string.length + if prefix.length < current_string.length + length = prefix.length + else + length = current_string.length + prefix = prefix[0...length] + end + length.times do |j| if prefix[j] != current_string[j] prefix = prefix[0...j] diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 4d0bc10..66927cb 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -39,5 +39,13 @@ expect(output).must_equal "" end + + it "will update shorter prefix strings" do + strings = ["bbb","bb"] + + output = longest_prefix(strings) + + expect(output).must_equal "bb" + end end end