-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2937-make-three-strings-equal.js
More file actions
35 lines (31 loc) · 950 Bytes
/
2937-make-three-strings-equal.js
File metadata and controls
35 lines (31 loc) · 950 Bytes
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
/**
* 2937. Make Three Strings Equal
* https://leetcode.com/problems/make-three-strings-equal/
* Difficulty: Easy
*
* You are given three strings: s1, s2, and s3. In one operation you can choose one of these
* strings and delete its rightmost character. Note that you cannot completely empty a string.
*
* Return the minimum number of operations required to make the strings equal. If it is
* impossible to make them equal, return -1.
*/
/**
* @param {string} s1
* @param {string} s2
* @param {string} s3
* @return {number}
*/
var findMinimumOperations = function(s1, s2, s3) {
const minLength = Math.min(s1.length, s2.length, s3.length);
let commonPrefixLength = 0;
for (let i = 0; i < minLength; i++) {
if (s1[i] !== s2[i] || s2[i] !== s3[i]) {
break;
}
commonPrefixLength++;
}
if (commonPrefixLength === 0) {
return -1;
}
return s1.length + s2.length + s3.length - 3 * commonPrefixLength;
};