File tree 6 files changed +179
-24
lines changed
solution/1500-1599/1544.Make The String Great
6 files changed +179
-24
lines changed Original file line number Diff line number Diff line change 61
61
62
62
<!-- 这里可写通用的实现逻辑 -->
63
63
64
+ ** 方法一:栈模拟**
65
+
66
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$,其中 $n$ 是字符串 ` s ` 的长度。
67
+
64
68
<!-- tabs:start -->
65
69
66
70
### ** Python3**
67
71
68
72
<!-- 这里可写当前语言的特殊实现逻辑 -->
69
73
70
74
``` python
71
-
75
+ class Solution :
76
+ def makeGood (self , s : str ) -> str :
77
+ stk = []
78
+ for c in s:
79
+ if not stk or abs (ord (stk[- 1 ]) - ord (c)) != 32 :
80
+ stk.append(c)
81
+ else :
82
+ stk.pop()
83
+ return " " .join(stk)
72
84
```
73
85
74
86
### ** Java**
75
87
76
88
<!-- 这里可写当前语言的特殊实现逻辑 -->
77
89
78
90
``` java
91
+ class Solution {
92
+ public String makeGood (String s ) {
93
+ StringBuilder sb = new StringBuilder ();
94
+ for (char c : s. toCharArray()) {
95
+ if (sb. length() == 0 || Math . abs(sb. charAt(sb. length() - 1 ) - c) != 32 ) {
96
+ sb. append(c);
97
+ } else {
98
+ sb. deleteCharAt(sb. length() - 1 );
99
+ }
100
+ }
101
+ return sb. toString();
102
+ }
103
+ }
104
+ ```
79
105
106
+ ### ** C++**
107
+
108
+ ``` cpp
109
+ class Solution {
110
+ public:
111
+ string makeGood(string s) {
112
+ string stk;
113
+ for (char c : s) {
114
+ if (stk.empty() || abs(stk.back() - c) != 32) {
115
+ stk += c;
116
+ } else {
117
+ stk.pop_back();
118
+ }
119
+ }
120
+ return stk;
121
+ }
122
+ };
80
123
```
81
124
125
+ ### **Go**
126
+
127
+ ```go
128
+ func makeGood(s string) string {
129
+ stk := []rune{}
130
+ for _, c := range s {
131
+ if len(stk) == 0 || abs(int(stk[len(stk)-1]-c)) != 32 {
132
+ stk = append(stk, c)
133
+ } else {
134
+ stk = stk[:len(stk)-1]
135
+ }
136
+ }
137
+ return string(stk)
138
+ }
139
+
140
+ func abs(x int) int {
141
+ if x < 0 {
142
+ return -x
143
+ }
144
+ return x
145
+ }
146
+ ```
147
+
148
+
82
149
### ** ...**
83
150
84
151
```
Original file line number Diff line number Diff line change 60
60
### ** Python3**
61
61
62
62
``` python
63
-
63
+ class Solution :
64
+ def makeGood (self , s : str ) -> str :
65
+ stk = []
66
+ for c in s:
67
+ if not stk or abs (ord (stk[- 1 ]) - ord (c)) != 32 :
68
+ stk.append(c)
69
+ else :
70
+ stk.pop()
71
+ return " " .join(stk)
64
72
```
65
73
66
74
### ** Java**
67
75
68
76
``` java
77
+ class Solution {
78
+ public String makeGood (String s ) {
79
+ StringBuilder sb = new StringBuilder ();
80
+ for (char c : s. toCharArray()) {
81
+ if (sb. length() == 0 || Math . abs(sb. charAt(sb. length() - 1 ) - c) != 32 ) {
82
+ sb. append(c);
83
+ } else {
84
+ sb. deleteCharAt(sb. length() - 1 );
85
+ }
86
+ }
87
+ return sb. toString();
88
+ }
89
+ }
90
+ ```
91
+
92
+ ### ** C++**
93
+
94
+ ``` cpp
95
+ class Solution {
96
+ public:
97
+ string makeGood(string s) {
98
+ string stk;
99
+ for (char c : s) {
100
+ if (stk.empty() || abs(stk.back() - c) != 32) {
101
+ stk += c;
102
+ } else {
103
+ stk.pop_back();
104
+ }
105
+ }
106
+ return stk;
107
+ }
108
+ };
109
+ ```
69
110
111
+ ### **Go**
112
+
113
+ ```go
114
+ func makeGood(s string) string {
115
+ stk := []rune{}
116
+ for _, c := range s {
117
+ if len(stk) == 0 || abs(int(stk[len(stk)-1]-c)) != 32 {
118
+ stk = append(stk, c)
119
+ } else {
120
+ stk = stk[:len(stk)-1]
121
+ }
122
+ }
123
+ return string(stk)
124
+ }
125
+
126
+ func abs(x int) int {
127
+ if x < 0 {
128
+ return -x
129
+ }
130
+ return x
131
+ }
70
132
```
71
133
72
134
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string makeGood (string s) {
4
+ string stk;
5
+ for (char c : s) {
6
+ if (stk.empty () || abs (stk.back () - c) != 32 ) {
7
+ stk += c;
8
+ } else {
9
+ stk.pop_back ();
10
+ }
11
+ }
12
+ return stk;
13
+ }
14
+ };
Original file line number Diff line number Diff line change
1
+ func makeGood (s string ) string {
2
+ stk := []rune {}
3
+ for _ , c := range s {
4
+ if len (stk ) == 0 || abs (int (stk [len (stk )- 1 ]- c )) != 32 {
5
+ stk = append (stk , c )
6
+ } else {
7
+ stk = stk [:len (stk )- 1 ]
8
+ }
9
+ }
10
+ return string (stk )
11
+ }
12
+
13
+ func abs (x int ) int {
14
+ if x < 0 {
15
+ return - x
16
+ }
17
+ return x
18
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public String makeGood (String s ) {
3
- return help (s );
4
- }
5
-
6
- private String help (String s ) {
7
- if (s == null || s .length () == 0 || s .length () == 1 ) {
8
- return s ;
9
- }
10
- char [] chars = s .toCharArray ();
11
- for (int i = 1 ; i < chars .length ; i ++) {
12
- if (Math .abs (chars [i ] - chars [i - 1 ]) == Math .abs ('A' - 'a' )) {
13
- return help (newString (chars , i ));
14
- }
15
- }
16
- return s ;
17
- }
18
-
19
- private String newString (char [] chars , int i ) {
20
- StringBuilder tmp = new StringBuilder ();
21
- for (int j = 0 ; j < chars .length ; j ++) {
22
- if (j != i && j != i - 1 ) {
23
- tmp .append (chars [j ]);
3
+ StringBuilder sb = new StringBuilder ();
4
+ for (char c : s .toCharArray ()) {
5
+ if (sb .length () == 0 || Math .abs (sb .charAt (sb .length () - 1 ) - c ) != 32 ) {
6
+ sb .append (c );
7
+ } else {
8
+ sb .deleteCharAt (sb .length () - 1 );
24
9
}
25
10
}
26
- return tmp .toString ();
11
+ return sb .toString ();
27
12
}
28
13
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def makeGood (self , s : str ) -> str :
3
+ stk = []
4
+ for c in s :
5
+ if not stk or abs (ord (stk [- 1 ]) - ord (c )) != 32 :
6
+ stk .append (c )
7
+ else :
8
+ stk .pop ()
9
+ return "" .join (stk )
You can’t perform that action at this time.
0 commit comments