-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0062.js
52 lines (38 loc) · 1.54 KB
/
solution0062.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
45
46
47
48
49
50
51
52
/*
--------------- 7 Kyu - Isograms ------------------
Instructions:
An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case.
Example: (Input --> Output)
"Dermatoglyphics" --> true
"aba" --> false
"moOse" --> false (ignore letter case)
-------------
Sample Tests
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Tests", () => {
it("test", () => {
assert.strictEqual( isIsogram("Dermatoglyphics"), true );
assert.strictEqual( isIsogram("isogram"), true );
assert.strictEqual( isIsogram("aba"), false, "same chars may not be adjacent" );
assert.strictEqual( isIsogram("moOse"), false, "same chars may not be same case" );
assert.strictEqual( isIsogram("isIsogram"), false );
assert.strictEqual( isIsogram(""), true, "an empty string is a valid isogram" );
});
});
--------------
Psuedo Code:
-use .map to see if x === x?
-.reduce()
-probably need to use .indexOf and .lastIndexOf to search through
-did not work, perhaps Regex..
-solutions do indeed show regex, alternately, the Set constructor, in which each key-value pair of the Set has a unique value. Still, not sure how it works with '.size' == 'str.length'
*/
function isIsogram(str){
return new Set(str.toUpperCase()).size == str.length;
}
//------------------------------
function isIsogram(str){
return !/(\w).*\1/i.test(str)
}