-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwo-strings.js
More file actions
64 lines (50 loc) · 1.08 KB
/
two-strings.js
File metadata and controls
64 lines (50 loc) · 1.08 KB
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
/*
*
* ------------------
* | TWO STRINGS |
* ------------------
*
*
* Given two strings, determine if they share a common substring.
* A substring may be as small as one character. For example, the
* words "a", "and", "art" share the common substring "a".
* The words "be" and "cat" do not share a substring. Substring can
* be as small as 1 char.
*
* e.g.
*
* numArr = [1, 3, 4 ]
*
* template(numArr) // returns 1 + 3 + 4
*
*
* */
/*
*
* ***** SOLUTION
*
* */
function twoStrings(s1,s2) {
// initialize a map to store values of s1
const stringMap = {};
// loop over s1 to populate map
for(let character of s1){
stringMap[character] = stringMap[character] + 1 || 1;
};
// loop over s2 to check if any value is in map
for(let character of s2) {
if(stringMap[character]) {
return "YES";
}
};
return "NO";
}
/*
*
* ***** TESTS
*
* */
console.log(twoStrings("km", "hello")); // NO
console.log(twoStrings("he", "hello")); // YES
console.log(twoStrings("how are you", "u")); // YES
console.log(twoStrings("atopr", "mylkj")); // NO