diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..bea8562 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,59 @@ -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - it will always run however long the list is. +# Space Complexity: O(1) - nothing new is created def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + + stored_item = nil + stored_index = 0 + new_array_length = 0 + current_index = 0 + + if list.length <= 1 + return list + end + + if list.length > 1 + until list[current_index - 1] == list.last && stored_item != nil + if stored_item == nil + stored_item = list[current_index] + list[stored_index] = list[current_index] + new_array_length += 1 + current_index += 1 + stored_index += 1 + elsif list[current_index] > stored_item + stored_item = list[current_index] + list[stored_index] = list[current_index] + new_array_length += 1 + current_index += 1 + stored_index += 1 + elsif list[current_index] == stored_item + current_index += 1 + end + end + end + + return list[0,new_array_length] + end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n*m) - or O(shortest_string.length * strings.length) +# Space Complexity: O(1) - def longest_prefix(strings) - raise NotImplementedError, "Not implemented yet" + shortest_string = strings.min_by { |string| string.length } + current_index = 0 + + until current_index == shortest_string.length + strings.each do |string| + if string[current_index] != shortest_string[current_index] + return shortest_string[0,current_index] + end + end + + current_index += 1 + + end + + return shortest_string[0,current_index] + end diff --git a/test/practice_exercises_test.rb b/test/practice_exercises_test.rb index 11d820b..d6bc913 100644 --- a/test/practice_exercises_test.rb +++ b/test/practice_exercises_test.rb @@ -32,6 +32,14 @@ expect(output).must_equal "f" end + it "returns the whole word if all match" do + strings = ["flower","flower","flower"] + + output = longest_prefix(strings) + + expect(output).must_equal "flower" + end + it "will work for the README strings" do strings = ["dog","racecar","car"]