Skip to content

Commit a980a4f

Browse files
authored
feat: add solutions to lc problem: No.3019 (doocs#2278)
No.3019.Number of Changing Keys
1 parent 6b8e4d6 commit a980a4f

File tree

7 files changed

+93
-70
lines changed

7 files changed

+93
-70
lines changed

solution/3000-3099/3019.Number of Changing Keys/README.md

+33-23
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,30 @@
4646

4747
## 解法
4848

49-
### 方法一
49+
### 方法一:一次遍历
50+
51+
我们可以遍历字符串,每次判断当前字符的小写形式是否与前一个字符的小写形式相同,如果不同则说明发生了按键变更,将答案加一即可。
52+
53+
时间复杂度 $O(n)$,其中 $n$ 是字符串 $s$ 的长度。空间复杂度 $O(1)$。
5054

5155
<!-- tabs:start -->
5256

5357
```python
5458
class Solution:
5559
def countKeyChanges(self, s: str) -> int:
56-
return sum(s.lower()[i] != s.lower()[i-1] for i in range(1, len(s)))
60+
return sum(a.lower() != b.lower() for a, b in pairwise(s))
5761
```
5862

5963
```java
6064
class Solution {
6165
public int countKeyChanges(String s) {
62-
int n = s.length();
63-
int count = 0;
64-
String lowerCaseString = s.toLowerCase();
65-
for (int i = 0; i < n - 1; i++) {
66-
if (lowerCaseString.charAt(i) != lowerCaseString.charAt(i + 1)) {
67-
count++;
66+
int ans = 0;
67+
for (int i = 1; i < s.length(); ++i) {
68+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
69+
++ans;
6870
}
6971
}
70-
return count;
72+
return ans;
7173
}
7274
}
7375
```
@@ -76,30 +78,38 @@ class Solution {
7678
class Solution {
7779
public:
7880
int countKeyChanges(string s) {
79-
int n = s.size();
80-
int c = 0;
8181
transform(s.begin(), s.end(), s.begin(), ::tolower);
82-
for (int i = 0; i < n - 1; i++) {
83-
if (s[i] != s[i + 1]) {
84-
c++;
85-
}
82+
int ans = 0;
83+
for (int i = 1; i < s.size(); ++i) {
84+
ans += s[i] != s[i - 1];
8685
}
87-
return c;
86+
return ans;
8887
}
8988
};
9089
```
9190
9291
```go
93-
func countKeyChanges(s string) int {
94-
n := len(s)
95-
count := 0
92+
func countKeyChanges(s string) (ans int) {
9693
s = strings.ToLower(s)
97-
for i := 0; i < n-1; i++ {
98-
if s[i] != s[i+1] {
99-
count++
94+
for i, c := range s[1:] {
95+
if byte(c) != s[i] {
96+
ans++
10097
}
10198
}
102-
return count
99+
return
100+
}
101+
```
102+
103+
```ts
104+
function countKeyChanges(s: string): number {
105+
s = s.toLowerCase();
106+
let ans = 0;
107+
for (let i = 1; i < s.length; ++i) {
108+
if (s[i] !== s[i - 1]) {
109+
++ans;
110+
}
111+
}
112+
return ans;
103113
}
104114
```
105115

solution/3000-3099/3019.Number of Changing Keys/README_EN.md

+33-23
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,30 @@ From s[4] = &#39;c&#39; to s[5] = &#39;C&#39;, there is no change of key as caps
4343

4444
## Solutions
4545

46-
### Solution 1
46+
### Solution 1: Single Pass
47+
48+
We can traverse the string, each time checking whether the lowercase form of the current character is the same as the lowercase form of the previous character. If they are different, it means that the key has been changed, and we can increment the answer accordingly.
49+
50+
The time complexity is $O(n)$, where $n$ is the length of the string $s$. The space complexity is $O(1)$.
4751

4852
<!-- tabs:start -->
4953

5054
```python
5155
class Solution:
5256
def countKeyChanges(self, s: str) -> int:
53-
return sum(s.lower()[i] != s.lower()[i-1] for i in range(1, len(s)))
57+
return sum(a.lower() != b.lower() for a, b in pairwise(s))
5458
```
5559

5660
```java
5761
class Solution {
5862
public int countKeyChanges(String s) {
59-
int n = s.length();
60-
int count = 0;
61-
String lowerCaseString = s.toLowerCase();
62-
for (int i = 0; i < n - 1; i++) {
63-
if (lowerCaseString.charAt(i) != lowerCaseString.charAt(i + 1)) {
64-
count++;
63+
int ans = 0;
64+
for (int i = 1; i < s.length(); ++i) {
65+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
66+
++ans;
6567
}
6668
}
67-
return count;
69+
return ans;
6870
}
6971
}
7072
```
@@ -73,30 +75,38 @@ class Solution {
7375
class Solution {
7476
public:
7577
int countKeyChanges(string s) {
76-
int n = s.size();
77-
int c = 0;
7878
transform(s.begin(), s.end(), s.begin(), ::tolower);
79-
for (int i = 0; i < n - 1; i++) {
80-
if (s[i] != s[i + 1]) {
81-
c++;
82-
}
79+
int ans = 0;
80+
for (int i = 1; i < s.size(); ++i) {
81+
ans += s[i] != s[i - 1];
8382
}
84-
return c;
83+
return ans;
8584
}
8685
};
8786
```
8887
8988
```go
90-
func countKeyChanges(s string) int {
91-
n := len(s)
92-
count := 0
89+
func countKeyChanges(s string) (ans int) {
9390
s = strings.ToLower(s)
94-
for i := 0; i < n-1; i++ {
95-
if s[i] != s[i+1] {
96-
count++
91+
for i, c := range s[1:] {
92+
if byte(c) != s[i] {
93+
ans++
9794
}
9895
}
99-
return count
96+
return
97+
}
98+
```
99+
100+
```ts
101+
function countKeyChanges(s: string): number {
102+
s = s.toLowerCase();
103+
let ans = 0;
104+
for (let i = 1; i < s.length; ++i) {
105+
if (s[i] !== s[i - 1]) {
106+
++ans;
107+
}
108+
}
109+
return ans;
100110
}
101111
```
102112

Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution {
22
public:
33
int countKeyChanges(string s) {
4-
int n = s.size();
5-
int c = 0;
64
transform(s.begin(), s.end(), s.begin(), ::tolower);
7-
for (int i = 0; i < n - 1; i++) {
8-
if (s[i] != s[i + 1]) {
9-
c++;
10-
}
5+
int ans = 0;
6+
for (int i = 1; i < s.size(); ++i) {
7+
ans += s[i] != s[i - 1];
118
}
12-
return c;
9+
return ans;
1310
}
14-
};
11+
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
func countKeyChanges(s string) int {
2-
n := len(s)
3-
count := 0
1+
func countKeyChanges(s string) (ans int) {
42
s = strings.ToLower(s)
5-
for i := 0; i < n-1; i++ {
6-
if s[i] != s[i+1] {
7-
count++
3+
for i, c := range s[1:] {
4+
if byte(c) != s[i] {
5+
ans++
86
}
97
}
10-
return count
8+
return
119
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
class Solution {
22
public int countKeyChanges(String s) {
3-
int n = s.length();
4-
int count = 0;
5-
String lowerCaseString = s.toLowerCase();
6-
for (int i = 0; i < n - 1; i++) {
7-
if (lowerCaseString.charAt(i) != lowerCaseString.charAt(i + 1)) {
8-
count++;
3+
int ans = 0;
4+
for (int i = 1; i < s.length(); ++i) {
5+
if (Character.toLowerCase(s.charAt(i)) != Character.toLowerCase(s.charAt(i - 1))) {
6+
++ans;
97
}
108
}
11-
return count;
9+
return ans;
1210
}
13-
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Solution:
22
def countKeyChanges(self, s: str) -> int:
3-
return sum(s.lower()[i] != s.lower()[i - 1] for i in range(1, len(s)))
3+
return sum(a.lower() != b.lower() for a, b in pairwise(s))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function countKeyChanges(s: string): number {
2+
s = s.toLowerCase();
3+
let ans = 0;
4+
for (let i = 1; i < s.length; ++i) {
5+
if (s[i] !== s[i - 1]) {
6+
++ans;
7+
}
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)