-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_permutations_and_combinations.rb
125 lines (101 loc) · 2.76 KB
/
string_permutations_and_combinations.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
require 'pry'
require 'pp'
# def permutation_on_lowercase(string)
# if string.length == 1
# return [string, string.upcase]
# end
# first = string[0]
# remaining = string[1..-1]
# for i in 0..remaining.length - 1
# result = []
# permutation_on_lowercase(remaining).each do |c|
# result << c + first
# result << c + first.upcase
# end
# result
# end
# result
# end
# puts permutation_on_lowercase('ab')
# puts permutation_on_lowercase('abc')
# sudo code:
# 1. return empty array if word size is 0
# return itself if word size is 1
# 2. split the first word from string, and put the reminder into the recursive function.
# 3. array of different formats should be returned from the resurive functions,
# for each word in this array, we want to insert the first word into different locations
# of the word
# 4. return different permutations of the word
def string_permutation(word)
return [] if word.size == 0
return [word] if word.size == 1
first = word[0]
reminder = word[1..-1]
words = string_permutation(reminder)
words.inject([]) do |results, word|
array = word.split('')
for i in 0..word.size
results << array.insert(i, first).join
array = word.split('')
end
results
end
end
puts string_permutation 'abc'
# def perms word
# return [] if word.size == 0
# return [word] if word.size == 1
# word.chars.each_with_index.map do |c,index|
# rest = word.dup
# rest.slice!(index)
# perms(rest).map do |perm|
# c + perm
# end
# end.flatten
# end
# puts perms 'abc'
# pp string_permutation('abcd')
# sudo code:
# 1. return empty array if word size is 0
# return itself if word size is 1
# 2. split first letter, and put reminder into recursive function
# results should be an array of reminder words combinations
# 3. interate each word in the results, and for each one,
# we can add the first letter into it or we can choose not to add the first letter.
# 4. return the combinations.
def comb word
return [] if word.size == 0
return [word] if word.size == 1
first = word[0]
reminder = word[1..-1]
words = comb(reminder)
words.inject([first]) do |memo, value|
memo << value + first
memo << value
memo
end
end
# @result = ''
# def comb_optimized word, index
# for i in index..word.length - 1
# @result += word[i]
# pp @result
# comb_optimized word, i+1
# @result = @result.slice(0..-2)
# end
# end
# @store = []
# def comb_optimized_two word
# for i in 0..word.length - 1
# if @store.any?
# store_dup = @store.dup
# store_dup.each do |comb|
# @store << comb + word[i]
# end
# end
# @store << word[i]
# end
# end
# # comb_optimized_two 'abc'
# comb_optimized_two 'wxyz'
# puts @store.size