-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1347.ts
56 lines (51 loc) · 1.54 KB
/
1347.ts
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
function minSteps(s: string, t: string): number {
let targetString = {}
let givenString = {}
let operation = 0
for (let i = 0; i < s.length; i++) {
if (targetString[s[i]]) targetString[s[i]] += 1
else targetString[s[i]] = 1
if (givenString[t[i]]) givenString[t[i]] += 1
else givenString[t[i]] = 1
}
for (let i in targetString) {
if (hasThisCharacter(givenString, i)) {
if (givenString[i] < targetString[i]) operation += (targetString[i] - givenString[i])
} else {
operation += targetString[i]
}
}
return operation
let i = 0, j = 0
while (j < t.length) {
if (!hasThisCharacter(givenString, t[j])) {
j++;
continue
} else if (!hasThisCharacter(targetString, t[j])) {
if (hasThisCharacter(givenString, s[i])) {
givenString = decreseCounter(givenString, s[i])
i++
continue
}
else {
operation++
targetString = decreseCounter(targetString, s[i])
}
}
else {
targetString = decreseCounter(targetString, t[j])
}
j++
i++
}
return operation
};
function hasThisCharacter(hash: Object, character: string) {
return hash[character] !== undefined
}
function decreseCounter(hash: Object, character: string) {
hash[character] -= 1
if (hash[character] == 0) delete hash[character]
return hash
}
console.log(minSteps('xxxz', 'aaxa'))