-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtext.ts
47 lines (36 loc) · 1.17 KB
/
text.ts
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
function solution(inputArray: string[]): boolean {
return loopArrays("", inputArray);
}
solution(["aba",
"bbb",
"bab"])
function loopArrays (currentStr: string, listStr: string[]): boolean {
if (listStr.length === 0) return true;
let canRearrange = false;
for (let i = 0 ; i < listStr.length ; i ++) {
const wordToCaparacao = listStr[i]
const hasOnlyOneDifferenc = countDiffChar(currentStr, wordToCaparacao) == 1
if (hasOnlyOneDifferenc) {
let clonedArray = [...listStr];
// remove a string de comparação nesse array
clonedArray.splice(i, 1);
clonedArray
if (loopArrays(wordToCaparacao, clonedArray)) {
canRearrange = true
};
}
}
return canRearrange;
}
function countDiffChar (str1: string, str2: string): number {
if (str1 === "") return 1;
let count = 0;
for (let i = 0 ; i < str1.length ; i ++) {
if (str1.charAt(i) !== str2.charAt(i)) count ++;
}
return count;
}
// gera um numero aleatorio de 0 a 1000
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}