-
Notifications
You must be signed in to change notification settings - Fork 15
/
TransformOneStringToAnother.js
58 lines (55 loc) · 1.55 KB
/
TransformOneStringToAnother.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
48
49
50
51
52
53
54
55
56
57
58
/**
* @author Anirudh Sharma
*
* Given two strings A and B, the task is to convert A to B if possible.
* The only operation allowed is to put any character from A and insert it at front.
*
* Find if it’s possible to convert the string. If yes, then output minimum no. of operations
* required for transformation.
*/
const minimumOperations = (A, B) => {
// Base checks for possible conversions
if (A.length !== B.length) {
return -1;
}
// Frequency map of characters in both strings
const frequencyMap = Array(256).fill(0);
// Loop through both strings
for (let i = 0; i < A.length; i++) {
frequencyMap[A.charCodeAt(i)]++;
frequencyMap[B.charCodeAt(i)]--;
}
// If frequency of any character is not zero,
// then we know that conversion is not possible
for (let num of frequencyMap) {
if (num !== 0) {
return -1;
}
}
// Count of operations needed
let count = 0;
// Variables to iterate through both strings
let indexA = A.length - 1;
let indexB = B.length - 1;
// Loop from right to left
while (indexA >= 0) {
// If the characters don't match,
// keep incrementing count
if (A.charAt(indexA) !== B.charAt(indexB)) {
count++;
} else {
indexB--;
}
indexA--;
}
return count;
};
const main = () => {
let A = "EACBD";
let B = "EABCD";
console.log(minimumOperations(A, B));
A = "ABD";
B = "BAD";
console.log(minimumOperations(A, B));
};
main();