forked from AdaGold/array-string-practice
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathpractice_exercises.rb
49 lines (43 loc) · 1.05 KB
/
practice_exercises.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Time Complexity: O(m)
# Space Complexity: O(1)
def remove_duplicates(list)
(list.length - 1).times do |count|
if list[count + 1] == list[count]
list[count + 1] = nil
end
end
(list.length - 1).times do |number|
if list[number] == nil
list[number] = list[number + 1]
list[number + 1] = nil
if list[number - 1] == nil
list[number - 1] = list[number]
list[number] = nil
end
end
end
return list
end
# Time Complexity: O(n*m)
# Space Complexity: O(1)
def longest_prefix(strings)
initial_match = ''
length = strings[0].length
length.times do |letter|
if strings[0][letter] == strings[1][letter]
initial_match.concat(strings[0][letter])
end
end
strings.each do |word|
match = ''
initial_match.length.times do |letter|
if initial_match[letter] == word[letter]
match.concat(word[letter])
end
end
initial_match = match
end
return initial_match
end
# p remove_duplicates([1, 2, 2, 3, 3, 4])
p longest_prefix(["flower","flow","flight"])