-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetcode 2914.java
35 lines (29 loc) · 1.06 KB
/
Leetcode 2914.java
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
class Solution {
public int minChanges(String s) {
// Initialize with first character of string
char currentChar = s.charAt(0);
int consecutiveCount = 0;
int minChangesRequired = 0;
// Iterate through each character in the string
for (int i = 0; i < s.length(); i++) {
// If current character matches the previous sequence
if (s.charAt(i) == currentChar) {
consecutiveCount++;
continue;
}
// If we have even count of characters, start new sequence
if (consecutiveCount % 2 == 0) {
consecutiveCount = 1;
}
// If odd count, we need to change current character
// to match previous sequence
else {
consecutiveCount = 0;
minChangesRequired++;
}
// Update current character for next iteration
currentChar = s.charAt(i);
}
return minChangesRequired;
}
}