-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path111.js
44 lines (36 loc) · 1.34 KB
/
111.js
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
/*
Dado un String, comprobar si todos los caracteres son unicos o no
* Input: `abcde` y `abcded`
* Output: `true` - `false`
*/
const comprobarCaracteres = (string) => {
// Creamos un set - Elimina valores repetidos
const comprobativo = new Set(string.toUpperCase());
// Si el size del set y del string (convertido a array) es lo mismo, entonces no cambio
return comprobativo.size === Array.from(string.toUpperCase()).length;
}
console.log(comprobarCaracteres("abcde")); // true
console.log(comprobarCaracteres("adee")); // false
console.log(comprobarCaracteres("adeE")); // false
// Solucion con hash
const comprobarCaracteres2 = (string) => {
// Creo el hash
const collection = new Map();
let found = true;
// Convierto el string a mayusculas y a array
const stringArray = Array.from(string.toUpperCase());
// recorro el array
stringArray.forEach(element => { // O(string)
// si el elemento ya se encuentra en el hash, entonces devolver false
if (collection.get(element)){
found = false;
return false;
}
// si no esta en el hash, agregarlo
collection.set(element, element);
})
return found;
}
console.log(comprobarCaracteres2("abcde")); // true
console.log(comprobarCaracteres2("adee")); // false
console.log(comprobarCaracteres2("adeE")); // false