Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions lib/practice_exercises.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: o(n)
# Space Complexity: o(1)
def remove_duplicates(list)
raise NotImplementedError, "Not implemented yet"
#raise NotImplementedError, "Not implemented yet"
array = list.length
index_1 = 0
array.times do
if list[index_1] == list[index_1 + 1]
list.delete_at(index_1)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually delete_at will remove the element at index_1 by shifting all the subsequent elements left by one index. So it's an O(n) method. Since you have it nested in a loop running n times. This is actually an O(n2) method.

else
index_1 +=1
end
end
return list
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: o(n^2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually O(mn) where m is the length of the strings and n is the number of strings.

# Space Complexity: o(1)
def longest_prefix(strings)
raise NotImplementedError, "Not implemented yet"
end
# raise NotImplementedError, "Not implemented yet"
return "" unless (strings.length > 0)
output = ""
first_w = strings[0]
i = 0
first_w.each_char do |letter|
if strings.all?{|string| string[i] == letter }

output += letter
i += 1
end
end
return output
end