-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242. Valid Anagram.js
47 lines (35 loc) · 1.05 KB
/
242. Valid Anagram.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
/**
* Given two strings s and t, return true if t is an anagram of s, and false otherwise.
*/
class Test {
method(text, anagram) {
if (text.length !== anagram.length)
return false;
const letters = [];
for (let char of text) {
letters.push(char);
}
for (let char of anagram) {
const i = letters.indexOf(char)
if (i === -1)
return false;
letters.splice(i, 1);
}
return letters.length === 0;
}
}
const testExecutor = new Test();
const testCase = [
{par1: 'anagram', par2: 'nagaram', result: true},
{par1: 'car', par2: 'rat', result: false},
{par1: 'invalid', par2: 'invalidd', result: false},
{par1: 'invalidd', par2: 'invalid', result: false},
{par1: 'longer', par2: 'sm', result: false},
{par1: 'smaller', par2: 'loooooonger', result: false},
];
testCase.forEach(testCase => {
console.log('Executing test', testCase);
const result = testExecutor.method(testCase.par1, testCase.par2);
console.log('Result:', result);
console.assert(result === testCase.result);
})