diff --git a/lib/practice_exercises.rb b/lib/practice_exercises.rb index 291e4e6..7aa213a 100644 --- a/lib/practice_exercises.rb +++ b/lib/practice_exercises.rb @@ -1,13 +1,45 @@ - -# Time Complexity: ? -# Space Complexity: ? +require 'pry' +# Time Complexity: O(n) +# Space Complexity: O(n) def remove_duplicates(list) - raise NotImplementedError, "Not implemented yet" + index = 0 + while index <= list.length + if list[index] == list[index + 1] + list.delete_at(index) + end + index += 1 + 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" + prefix = strings[0] + original = prefix + a_length = strings.length + a_length -= 1 + + + a_length.times do |index| + comparison_word = strings[index + 1] + w_length = prefix.length + w_length.times do |i| + if prefix[i] != comparison_word[i] + i -= 1 + prefix = prefix[0..i] + if prefix == original + return "" + end + break + end + end + end + return prefix end + +