Skip to content

Commit f22a25b

Browse files
solve #3019: Number of Changing Keys in java
1 parent 6c8ca95 commit f22a25b

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@
884884
| 3005 | [Count Elements With Maximum Frequency](https://leetcode.com/problems/count-elements-with-maximum-frequency) | [![Java](assets/java.png)](src/CountElementsWithMaximumFrequency.java) | |
885885
| 3010 | [Divide an Array Into Subarrays With Minimum Cost I](https://leetcode.com/problems/divide-an-array-into-subarrays-with-minimum-cost-i) | [![Java](assets/java.png)](src/DivideAnArrayIntoSubarraysWithMinimumCostI.java) | |
886886
| 3014 | [Minimum Number of Pushes to Type Word I](https://leetcode.com/problems/minimum-number-of-pushes-to-type-word-i) | [![Java](assets/java.png)](src/MinimumNumberOfPushesToTypeWordI.java) | |
887-
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys) | | |
887+
| 3019 | [Number of Changing Keys](https://leetcode.com/problems/number-of-changing-keys) | [![Java](assets/java.png)](src/NumberOfChangingKeys.java) | |
888888
| 3024 | [Type of Triangle](https://leetcode.com/problems/type-of-triangle) | | |
889889
| 3028 | [Ant on the Boundary](https://leetcode.com/problems/ant-on-the-boundary) | | |
890890
| 3032 | 🔒 [Count Numbers With Unique Digits II](https://leetcode.com/problems/count-numbers-with-unique-digits-ii) | | |

Diff for: src/NumberOfChangingKeys.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// https://leetcode.com/problems/number-of-changing-keys
2+
// T: O(|s|)
3+
// S: O(1)
4+
5+
public class NumberOfChangingKeys {
6+
public int countKeyChanges(String s) {
7+
int changes = 0;
8+
for (int i = 1 ; i < s.length() ; i++) {
9+
if (isDifferent(s.charAt(i - 1), s.charAt(i))) {
10+
changes++;
11+
}
12+
}
13+
return changes;
14+
}
15+
16+
private static boolean isDifferent(char a, char b) {
17+
return Character.toLowerCase(a) != Character.toLowerCase(b);
18+
}
19+
}

0 commit comments

Comments
 (0)