-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path60-strings-mix.js
More file actions
61 lines (60 loc) · 2.93 KB
/
60-strings-mix.js
File metadata and controls
61 lines (60 loc) · 2.93 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
// Given two strings s1 and s2, we want to visualize how different the two strings are.
// We will only take into account the lowercase letters (a to z). First let us count the
// frequency of each lowercase letters in s1 and s2.
// s1 = "A aaaa bb c"
// s2 = "& aaa bbb c d"
// s1 has 4 'a', 2 'b', 1 'c'
// s2 has 3 'a', 3 'b', 1 'c', 1 'd'
// So the maximum for 'a' in s1 and s2 is 4 from s1; the maximum for 'b' is 3 from s2.
// In the following we will not consider letters when the maximum of their occurrences
// is less than or equal to 1.
//
// We can resume the differences between s1 and s2 in the following string: "1:aaaa/2:bbb"
// where 1 in 1:aaaa stands for string s1 and aaaa because the maximum for a is 4.
// In the same manner 2:bbb stands for string s2 and bbb because the maximum for b is 3.
//
// The task is to produce a string in which each lowercase letters of s1 or s2 appears as
// many times as its maximum if this maximum is strictly greater than 1; these letters will
// be prefixed by the number of the string where they appear with their maximum value
// and :. If the maximum is in s1 as well as in s2 the prefix is '=:'.
//
// In the result, substrings (a substring is for example 2:nnnnn or 1:hhh; it contains the prefix)
// will be in decreasing order of their length and when they have the same length sorted in
// ascending lexicographic order (letters and digits - more precisely sorted by codepoint);
// the different groups will be separated by '/'. See examples and "Example Tests".
//
// Hopefully other examples can make this clearer.
//
// s1 = "my&friend&Paul has heavy hats! &"
// s2 = "my friend John has many many friends &"
// mix(s1, s2) --> "2:nnnnn/1:aaaa/1:hhh/2:mmm/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"
//
// s1 = "mmmmm m nnnnn y&friend&Paul has heavy hats! &"
// s2 = "my frie n d Joh n has ma n y ma n y frie n ds n&"
// mix(s1, s2) --> "1:mmmmmm/=:nnnnnn/1:aaaa/1:hhh/2:yyy/2:dd/2:ff/2:ii/2:rr/=:ee/=:ss"
//
// s1="Are the kids at home? aaaaa fffff"
// s2="Yes they are here! aaaaa fffff"
// mix(s1, s2) --> "=:aaaaaa/2:eeeee/=:fffff/1:tt/2:rr/=:hh"
export default (s1, s2) => {
const toObj = s => s.split('').reduce((a, e) => (/[^a-z]/.test(e) ? a : { ...a, [e]: 1 + (a[e] ? a[e] : 0) }), {});
const [obj1, obj2] = [toObj(s1), toObj(s2)];
return Object.keys({ ...obj1, ...obj2 })
.reduce((a, e) => {
if ((!obj1[e] || obj1[e] < 2) && (!obj2[e] || obj2[e] < 2)) return a;
if (!obj2[e] || obj1[e] > obj2[e]) return [...a, { o: 1, e, n: obj1[e] }];
if (!obj1[e] || obj2[e] > obj1[e]) return [...a, { o: 2, e, n: obj2[e] }];
return [...a, { o: '=', e, n: obj1[e] }];
}, [])
.sort(({ o: o1, e: e1, n: n1 }, { o: o2, e: e2, n: n2 }) => {
if (n1 !== n2) return n2 - n1;
if (o1 !== o2) {
if (o1 === '=') return 1;
if (o2 === '=') return -1;
return o1 - o2;
}
return e2 > e1 ? -1 : 1;
})
.map(({ o, e, n }) => `${o}:${e.repeat(n)}`)
.join('/');
};