Skip to content

Commit d3c9afa

Browse files
Added solution for min moves
1 parent a34f347 commit d3c9afa

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Happy to accept any contributions to this in any langugage.
5959
24. [Leet Code 151 - Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/) | [Solution](level_medium/LeetCode_151_Reverse_Words_In_a_String.ts)
6060
25. [Leet Code 1647 - Minimum Deletions to Make Character Frequency Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/) | [Solution](level_medium/LeetCode_1647_Min_Deletions_For_Unique_Freq.ts)
6161
26. [Microsoft OA - Max Sum of Numbers with Equal Sum Digits](https://leetcode.com/discuss/interview-question/915683/) | [Solution](./level_medium/MaxSumOfNumbersWithEqualSumOfDigit.ts)
62+
27. [Microsoft OA - Min Moves to Make String without 3 Identical Chars](https://molchevskyi.medium.com/microsoft-interview-tasks-min-moves-to-make-string-without-3-identical-consecutive-letters-abe61ed51a10) | [Solution](./level_medium/MinMovesToMakeStringWithout3IdenticalChars.ts)
6263

6364

6465
## Hard
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { count, countReset } from "console";
2+
3+
/**
4+
* https://leetcode.com/discuss/interview-question/398026/
5+
*
6+
* Solution uses the sliding window pattern. Here, the sliding window is created for the entire length of the
7+
* consequtive letters. The sliding window can be fixed, but that is not an optimal correct solution. The sliding
8+
* window with repeated characters allow for choosing the min possible swaps which would only be repeat/3
9+
*
10+
* Time: O(n), Space: O(1)
11+
*
12+
* @param input
13+
*/
14+
function findMinMoves(input: string): number {
15+
16+
if (input.length < 3) return 0;
17+
let last = '', curr = '', swaps = 0, counter = 1, idx = 0
18+
while(true) {
19+
curr = input[idx] || ''
20+
if (last === curr) counter++
21+
else {
22+
if (counter >= 3) {
23+
swaps += Math.floor(counter / 3)
24+
}
25+
counter = 1
26+
}
27+
last = curr
28+
idx++
29+
if (curr === '') break;
30+
}
31+
32+
return swaps;
33+
}
34+
35+
console.log(findMinMoves("baaaaa"))
36+
console.log(findMinMoves("baaabbaabbba"))
37+
console.log(findMinMoves("baabab"))
38+
console.log(findMinMoves("baaaab"))
39+
console.log(findMinMoves("baaaaaab"))
40+
console.log(findMinMoves("baaaaaaaaaab"))

0 commit comments

Comments
 (0)