Skip to content

Commit 6c71a45

Browse files
authored
Create 2800-shortest-string-that-contains-three-strings.js
1 parent b765470 commit 6c71a45

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const lexical_smallest_comp = (x, y) => x < y ? -1 : x > y ? 1 : 0;
2+
const merge = (s, t) => {
3+
if (s.indexOf(t) != -1) return s;
4+
// concat based on suffix
5+
for (let l = Math.min(s.length, t.length); l > 0; l--) {
6+
if (s.slice(-l) == t.slice(0, l)) return s.slice(0, -l) + t;
7+
}
8+
return s + t;
9+
};
10+
/**
11+
* @param {string} a
12+
* @param {string} b
13+
* @param {string} c
14+
* @return {string}
15+
*/
16+
const minimumString = (a, b, c) => {
17+
let d = [merge(merge(a, b), c), merge(merge(a, c), b), merge(merge(b, a), c), merge(merge(b, c), a), merge(merge(c, a), b), merge(merge(c, b), a)];
18+
d.sort((x, y) => {
19+
if (x.length != y.length) return x.length - y.length;
20+
return lexical_smallest_comp(x, y);
21+
})
22+
return d[0];
23+
};

0 commit comments

Comments
 (0)