Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e3f7ac0

Browse files
authored
feat: add solutions to lc problem: No.3017 (doocs#2268)
1 parent ea13071 commit e3f7ac0

File tree

6 files changed

+123
-8
lines changed

6 files changed

+123
-8
lines changed

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,56 @@
5151
<!-- tabs:start -->
5252

5353
```python
54-
54+
class Solution:
55+
def countKeyChanges(self, s: str) -> int:
56+
return sum(s.lower()[i] != s.lower()[i-1] for i in range(1, len(s)))
5557
```
5658

5759
```java
58-
60+
class Solution {
61+
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++;
68+
}
69+
}
70+
return count;
71+
}
72+
}
5973
```
6074

6175
```cpp
62-
76+
class Solution {
77+
public:
78+
int countKeyChanges(string s) {
79+
int n = s.size();
80+
int c = 0;
81+
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+
}
86+
}
87+
return c;
88+
}
89+
};
6390
```
6491
6592
```go
66-
93+
func countKeyChanges(s string) int {
94+
n := len(s)
95+
count := 0
96+
s = strings.ToLower(s)
97+
for i := 0; i < n-1; i++ {
98+
if s[i] != s[i+1] {
99+
count++
100+
}
101+
}
102+
return count
103+
}
67104
```
68105

69106
<!-- tabs:end -->

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

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,56 @@ From s[4] = &#39;c&#39; to s[5] = &#39;C&#39;, there is no change of key as caps
4848
<!-- tabs:start -->
4949

5050
```python
51-
51+
class Solution:
52+
def countKeyChanges(self, s: str) -> int:
53+
return sum(s.lower()[i] != s.lower()[i-1] for i in range(1, len(s)))
5254
```
5355

5456
```java
55-
57+
class Solution {
58+
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++;
65+
}
66+
}
67+
return count;
68+
}
69+
}
5670
```
5771

5872
```cpp
59-
73+
class Solution {
74+
public:
75+
int countKeyChanges(string s) {
76+
int n = s.size();
77+
int c = 0;
78+
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+
}
83+
}
84+
return c;
85+
}
86+
};
6087
```
6188
6289
```go
63-
90+
func countKeyChanges(s string) int {
91+
n := len(s)
92+
count := 0
93+
s = strings.ToLower(s)
94+
for i := 0; i < n-1; i++ {
95+
if s[i] != s[i+1] {
96+
count++
97+
}
98+
}
99+
return count
100+
}
64101
```
65102

66103
<!-- tabs:end -->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int countKeyChanges(string s) {
4+
int n = s.size();
5+
int c = 0;
6+
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+
}
11+
}
12+
return c;
13+
}
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func countKeyChanges(s string) int {
2+
n := len(s)
3+
count := 0
4+
s = strings.ToLower(s)
5+
for i := 0; i < n-1; i++ {
6+
if s[i] != s[i+1] {
7+
count++
8+
}
9+
}
10+
return count
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
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++;
9+
}
10+
}
11+
return count;
12+
}
13+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def countKeyChanges(self, s: str) -> int:
3+
return sum(s.lower()[i] != s.lower()[i - 1] for i in range(1, len(s)))

0 commit comments

Comments
 (0)