-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstringsRearrangement.R
executable file
·225 lines (215 loc) · 6.81 KB
/
stringsRearrangement.R
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# Given an array of equal-length strings,
# you'd like to know if it's possible to
# rearrange the order of the elements in
# such a way that each consecutive pair
# of strings differ by exactly one character.
# Return true if it's possible, and false if not.
#
# Note: You're only rearranging the order of the strings, not the order of the letters within the strings!
#
# Example
#
# For inputArray = ["aba", "bbb", "bab"], the output should be
# stringsRearrangement(inputArray) = false.
#
# There are 6 possible arrangements for these strings:
# ["aba", "bbb", "bab"]
# ["aba", "bab", "bbb"]
# ["bbb", "aba", "bab"]
# ["bbb", "bab", "aba"]
# ["bab", "bbb", "aba"]
# ["bab", "aba", "bbb"]
#
# None of these satisfy the condition of consecutive strings differing by 1 character, so the answer is false.
#
# For inputArray = ["ab", "bb", "aa"], the output should be
# stringsRearrangement(inputArray) = true.
#
# It's possible to arrange these strings in a way that each consecutive pair of strings differ by 1 character (eg: "aa", "ab", "bb" or "bb", "ab", "aa"), so return true.
#
# inputArray = list("aba",
# "bbb",
# "bab")
#
# inputArray = list("ab", "bb", "aa")
#
# inputArray = list("abc",
# "bef",
# "bcc",
# "bec",
# "bbc",
# "bdc")
# inputArray = list(
# "ff",
# "gf",
# "af",
# "ar",
# "hf"
# )
#todo: write own permutation logic:
#big mistake I was making till now, was not storing the permutation indices but computing
#them each time and extracting the specific row. :( Spent hours/days.. over this
stringsRearrangement <- function(inputArray) {
inputArray <- unlist(inputArray)
# https://stackoverflow.com/questions/11095992/generating-all-distinct-permutations-of-a-list-in-r
permutations <- function(n){
if(n==1){
return(matrix(1))
} else {
sp <- permutations(n-1)
p <- nrow(sp)
A <- matrix(nrow=n*p,ncol=n)
for(i in 1:n){
A[(i-1)*p+1:p,] <- cbind(i,sp+(sp>=i))
}
return(A)
}
}
permutationindices <- permutations(length(inputArray))
for (ind in 1:factorial(length(inputArray))) {
#get the values in that order
combinationitn <- inputArray[permutationindices[ind,]]
diffarray = 0;
for (ind2 in 1:(length(combinationitn)-1)) {
if (sum(strsplit(combinationitn[ind2],"")[[1]] != strsplit(combinationitn[ind2 + 1],"")[[1]]) == 1) {
diffarray = diffarray + 1
} else {
break
}
}
if (diffarray == (length(inputArray) - 1)) {
return(TRUE)
}
}
return(FALSE)
}
#still not fast enough: die die
# stringsRearrangement <- function(inputArray) {
# inputArray <- unlist(inputArray)
# # https://stackoverflow.com/questions/11095992/generating-all-distinct-permutations-of-a-list-in-r
#
# permutations <- function(n){
# if(n==1){
# return(matrix(1))
# } else {
# sp <- permutations(n-1)
# p <- nrow(sp)
# A <- matrix(nrow=n*p,ncol=n)
# for(i in 1:n){
# A[(i-1)*p+1:p,] <- cbind(i,sp+(sp>=i))
# }
# return(A)
# }
# }
#
# for (ind in 1:factorial(length(inputArray))) {
# permutationindices <- permutations(length(inputArray))[ind,]
# #get the values in that order
# combinationitn <- inputArray[permutationindices]
#
# combinationitn <- lapply(combinationitn,utf8ToInt)
# diffarray <- lapply(seq(1:(length(combinationitn) - 1)), function(x) {
# return((combinationitn[[x]] - combinationitn[[x+1]]))
# })
#
# nonzeroelements <- lapply(diffarray,function(x) {
# return(sum(x != 0))
# })
# if (all(abs(unlist(nonzeroelements)) == 1)) {
# return(TRUE)
# }
# }
# return(FALSE)
# }
#combinat library is not there on codesignal.
# library(combinat)
# stringsRearrangement <- function(inputArray) {
# inputArray <- unlist(inputArray)
# allcombinations <- permn(inputArray)
#
# output <- FALSE
# for (x in seq(1:length(allcombinations))) {
# speccombination <- allcombinations[[x]]
# if (output == FALSE) {
# alldiffwithinthecombination <- lapply(seq(1:(length(speccombination)-1)),function(y) {
# sum(strsplit(speccombination[y],"")[[1]] != strsplit(speccombination[y+1],"")[[1]])
# })
# if (all(alldiffwithinthecombination == 1)) {
# output <- TRUE
# break
# }
# }
# }
# return(output)
# }
# stringsRearrangement <- function(inputArray) {
# #convert string to int encoding
# inputArray <- lapply(inputArray,utf8ToInt)
# # inputArray <- as.vector(unlist(inputArray))
# # inputArray <- strsplit(inputArray,"")
#
# #check number of different bits in the arrangement.
# diffarray <- lapply(seq(1:(length(inputArray) - 1)), function(x) {
# return((inputArray[[x]] - inputArray[[x+1]]))
# })
# #rules:
# #for true
# #1. length of diffarray = 1 element and it is 1
# #2. either each row has only 1 nonzero element and it is abs() == 1
# #3. do columnsum do there's only 1 element which is abs() >= 1.
#
# #check for rule1
# if (length(diffarray) == 1 && all(diffarray == 0)) {
# return(FALSE)
# }
# #check for rule2
# nonzeroelements <- lapply(diffarray,function(x) {
# return(sum(x != 0))
# })
# if (all(abs(unlist(nonzeroelements)) == 1)) {
# return(TRUE)
# }
# #check for other rules
# columnsums <- colSums(do.call(rbind,diffarray))
# if (sum(columnsums) == 1) {
# return(TRUE)
# } else {
# return(FALSE)
# }
# }
##old
# stringsRearrangement <- function(inputArray) {
# #convert string to int encoding
# inputArray <- lapply(inputArray,utf8ToInt)
# # inputArray <- as.vector(unlist(inputArray))
# # inputArray <- strsplit(inputArray,"")
#
# #check number of different bits in the arrangement.
# diffarray <- lapply(seq(1:(length(inputArray) - 1)), function(x) {
# return((inputArray[[x]] - inputArray[[x+1]]))
# })
# #rules:
# #for true
# #1. length of diffarray = 1 element and it is 1
# #2. either each row has only 1 nonzero element and it is abs() == 1
# #3. do columnsum do there's only 1 element which is abs() >= 1.
#
# #check for rule1
# if (length(diffarray) == 1 && all(diffarray == 0)) {
# return(FALSE)
# }
# #check for rule2
# nonzeroelements <- lapply(diffarray,function(x) {
# return(sum(x != 0))
# })
# if (all(abs(unlist(nonzeroelements)) == 1)) {
# return(TRUE)
# }
# #check for other rules
# columnsums <- colSums(do.call(rbind,diffarray))
# if ((sum(abs(columnsums) >= 1) == 1) || (any(columnsums == 0) && (sum(abs(columnsums) > 1) == 1))) {
# return(TRUE)
# } else {
# return(FALSE)
# }
# }