diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..bdfefc0 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,60 @@ -# 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] + + 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] + break + end + end + end + end + + return prefix end 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