Skip to content

Commit e3f523d

Browse files
committed
feat: add solutions to lc problem: No.2027
No.2027.Minimum Moves to Convert String
1 parent c20dc0c commit e3f523d

File tree

6 files changed

+159
-2
lines changed

6 files changed

+159
-2
lines changed

solution/2000-2099/2027.Minimum Moves to Convert String/README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,81 @@
5353

5454
<!-- 这里可写通用的实现逻辑 -->
5555

56+
**方法一:贪心**
57+
58+
遍历字符串 $s$,只要遇到 `X`,指针就直接往后移动三格,并且答案加 $1$;否则指针往后移动一格。
59+
60+
时间复杂度 $O(n)$。其中 $n$ 表示字符串 $s$ 的长度。
61+
5662
<!-- tabs:start -->
5763

5864
### **Python3**
5965

6066
<!-- 这里可写当前语言的特殊实现逻辑 -->
6167

6268
```python
63-
69+
class Solution:
70+
def minimumMoves(self, s: str) -> int:
71+
ans = i = 0
72+
while i < len(s):
73+
if s[i] == "X":
74+
ans += 1
75+
i += 3
76+
else:
77+
i += 1
78+
return ans
6479
```
6580

6681
### **Java**
6782

6883
<!-- 这里可写当前语言的特殊实现逻辑 -->
6984

7085
```java
86+
class Solution {
87+
public int minimumMoves(String s) {
88+
int ans = 0;
89+
for (int i = 0; i < s.length(); ++i) {
90+
if (s.charAt(i) == 'X') {
91+
++ans;
92+
i += 2;
93+
}
94+
}
95+
return ans;
96+
}
97+
}
98+
```
99+
100+
### **C++**
101+
102+
```cpp
103+
class Solution {
104+
public:
105+
int minimumMoves(string s) {
106+
int ans = 0;
107+
for (int i = 0; i < s.size(); ++i) {
108+
if (s[i] == 'X') {
109+
++ans;
110+
i += 2;
111+
}
112+
}
113+
return ans;
114+
}
115+
};
116+
```
71117
118+
### **Go**
119+
120+
```go
121+
func minimumMoves(s string) int {
122+
ans := 0
123+
for i := 0; i < len(s); i++ {
124+
if s[i] == 'X' {
125+
ans++
126+
i += 2
127+
}
128+
}
129+
return ans
130+
}
72131
```
73132

74133
### **...**

solution/2000-2099/2027.Minimum Moves to Convert String/README_EN.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,66 @@ Then we select the last 3 characters and convert them so that the final string c
5252
### **Python3**
5353

5454
```python
55-
55+
class Solution:
56+
def minimumMoves(self, s: str) -> int:
57+
ans = i = 0
58+
while i < len(s):
59+
if s[i] == "X":
60+
ans += 1
61+
i += 3
62+
else:
63+
i += 1
64+
return ans
5665
```
5766

5867
### **Java**
5968

6069
```java
70+
class Solution {
71+
public int minimumMoves(String s) {
72+
int ans = 0;
73+
for (int i = 0; i < s.length(); ++i) {
74+
if (s.charAt(i) == 'X') {
75+
++ans;
76+
i += 2;
77+
}
78+
}
79+
return ans;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
int minimumMoves(string s) {
90+
int ans = 0;
91+
for (int i = 0; i < s.size(); ++i) {
92+
if (s[i] == 'X') {
93+
++ans;
94+
i += 2;
95+
}
96+
}
97+
return ans;
98+
}
99+
};
100+
```
61101
102+
### **Go**
103+
104+
```go
105+
func minimumMoves(s string) int {
106+
ans := 0
107+
for i := 0; i < len(s); i++ {
108+
if s[i] == 'X' {
109+
ans++
110+
i += 2
111+
}
112+
}
113+
return ans
114+
}
62115
```
63116

64117
### **...**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int minimumMoves(string s) {
4+
int ans = 0;
5+
for (int i = 0; i < s.size(); ++i) {
6+
if (s[i] == 'X') {
7+
++ans;
8+
i += 2;
9+
}
10+
}
11+
return ans;
12+
}
13+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func minimumMoves(s string) int {
2+
ans := 0
3+
for i := 0; i < len(s); i++ {
4+
if s[i] == 'X' {
5+
ans++
6+
i += 2
7+
}
8+
}
9+
return ans
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution {
2+
public int minimumMoves(String s) {
3+
int ans = 0;
4+
for (int i = 0; i < s.length(); ++i) {
5+
if (s.charAt(i) == 'X') {
6+
++ans;
7+
i += 2;
8+
}
9+
}
10+
return ans;
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def minimumMoves(self, s: str) -> int:
3+
ans = i = 0
4+
while i < len(s):
5+
if s[i] == "X":
6+
ans += 1
7+
i += 3
8+
else:
9+
i += 1
10+
return ans

0 commit comments

Comments
 (0)