File tree 9 files changed +242
-8
lines changed
0800-0899/0870.Advantage Shuffle
1900-1999/1933.Check if String Is Decomposable Into Value-Equal Substrings
9 files changed +242
-8
lines changed Original file line number Diff line number Diff line change 56
56
class Solution :
57
57
def advantageCount (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
58
58
nums1.sort()
59
- t = [(v, i) for i, v in enumerate (nums2)]
60
- t.sort()
59
+ t = sorted ((v, i) for i, v in enumerate (nums2))
61
60
n = len (nums2)
62
61
ans = [0 ] * n
63
62
i, j = 0 , n - 1
Original file line number Diff line number Diff line change 35
35
class Solution :
36
36
def advantageCount (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
37
37
nums1.sort()
38
- t = [(v, i) for i, v in enumerate (nums2)]
39
- t.sort()
38
+ t = sorted ((v, i) for i, v in enumerate (nums2))
40
39
n = len (nums2)
41
40
ans = [0 ] * n
42
41
i, j = 0 , n - 1
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def advantageCount (self , nums1 : List [int ], nums2 : List [int ]) -> List [int ]:
3
3
nums1 .sort ()
4
- t = [(v , i ) for i , v in enumerate (nums2 )]
5
- t .sort ()
4
+ t = sorted ((v , i ) for i , v in enumerate (nums2 ))
6
5
n = len (nums2 )
7
6
ans = [0 ] * n
8
7
i , j = 0 , n - 1
Original file line number Diff line number Diff line change 55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ ** 方法一:双指针**
59
+
60
+ 遍历字符串 $s$,用双指针 $i$ 和 $j$ 统计每个等值子字符串的长度。若长度模 $3$ 余 $1$,说明该子字符串长度不符合要求,返回 ` false ` ;若长度模 $3$ 余 $2$,说明出现了长度为 $2$ 的子字符串,若此前已经出现过长度为 $2$ 的子字符串,返回 ` false ` ,否则将 $j$ 的值赋给 $i$,继续遍历。
61
+
62
+ 遍历结束后,判断是否出现过长度为 $2$ 的子字符串,若没有,返回 ` false ` ,否则返回 ` true ` 。
63
+
64
+ 时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
65
+
58
66
<!-- tabs:start -->
59
67
60
68
### ** Python3**
61
69
62
70
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
71
64
72
``` python
65
-
73
+ class Solution :
74
+ def isDecomposable (self , s : str ) -> bool :
75
+ i, n = 0 , len (s)
76
+ cnt2 = 0
77
+ while i < n:
78
+ j = i
79
+ while j < n and s[j] == s[i]:
80
+ j += 1
81
+ if (j - i) % 3 == 1 :
82
+ return False
83
+ cnt2 += (j - i) % 3 == 2
84
+ if cnt2 > 1 :
85
+ return False
86
+ i = j
87
+ return cnt2 == 1
66
88
```
67
89
68
90
### ** Java**
69
91
70
92
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
93
72
94
``` java
95
+ class Solution {
96
+ public boolean isDecomposable (String s ) {
97
+ int i = 0 , n = s. length();
98
+ int cnt2 = 0 ;
99
+ while (i < n) {
100
+ int j = i;
101
+ while (j < n && s. charAt(j) == s. charAt(i)) {
102
+ ++ j;
103
+ }
104
+ if ((j - i) % 3 == 1 ) {
105
+ return false ;
106
+ }
107
+ if ((j - i) % 3 == 2 && ++ cnt2 > 1 ) {
108
+ return false ;
109
+ }
110
+ i = j;
111
+ }
112
+ return cnt2 == 1 ;
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ class Solution {
121
+ public:
122
+ bool isDecomposable(string s) {
123
+ int i = 0, n = s.size();
124
+ int cnt2 = 0;
125
+ while (i < n) {
126
+ int j = i;
127
+ while (j < n && s[ j] == s[ i] ) ++j;
128
+ if ((j - i) % 3 == 1) return false;
129
+ if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
130
+ i = j;
131
+ }
132
+ return cnt2 == 1;
133
+ }
134
+ };
135
+ ```
73
136
137
+ ### **Go**
138
+
139
+ ```go
140
+ func isDecomposable(s string) bool {
141
+ i, n := 0, len(s)
142
+ cnt2 := 0
143
+ for i < n {
144
+ j := i
145
+ for j < n && s[j] == s[i] {
146
+ j++
147
+ }
148
+ if (j-i)%3 == 1 {
149
+ return false
150
+ }
151
+ if (j-i)%3 == 2 {
152
+ cnt2++
153
+ if cnt2 > 1 {
154
+ return false
155
+ }
156
+ }
157
+ i = j
158
+ }
159
+ return cnt2 == 1
160
+ }
74
161
```
75
162
76
163
### ** ...**
Original file line number Diff line number Diff line change 57
57
### ** Python3**
58
58
59
59
``` python
60
-
60
+ class Solution :
61
+ def isDecomposable (self , s : str ) -> bool :
62
+ i, n = 0 , len (s)
63
+ cnt2 = 0
64
+ while i < n:
65
+ j = i
66
+ while j < n and s[j] == s[i]:
67
+ j += 1
68
+ if (j - i) % 3 == 1 :
69
+ return False
70
+ cnt2 += (j - i) % 3 == 2
71
+ if cnt2 > 1 :
72
+ return False
73
+ i = j
74
+ return cnt2 == 1
61
75
```
62
76
63
77
### ** Java**
64
78
65
79
``` java
80
+ class Solution {
81
+ public boolean isDecomposable (String s ) {
82
+ int i = 0 , n = s. length();
83
+ int cnt2 = 0 ;
84
+ while (i < n) {
85
+ int j = i;
86
+ while (j < n && s. charAt(j) == s. charAt(i)) {
87
+ ++ j;
88
+ }
89
+ if ((j - i) % 3 == 1 ) {
90
+ return false ;
91
+ }
92
+ if ((j - i) % 3 == 2 && ++ cnt2 > 1 ) {
93
+ return false ;
94
+ }
95
+ i = j;
96
+ }
97
+ return cnt2 == 1 ;
98
+ }
99
+ }
100
+ ```
101
+
102
+ ### ** C++**
103
+
104
+ ``` cpp
105
+ class Solution {
106
+ public:
107
+ bool isDecomposable(string s) {
108
+ int i = 0, n = s.size();
109
+ int cnt2 = 0;
110
+ while (i < n) {
111
+ int j = i;
112
+ while (j < n && s[ j] == s[ i] ) ++j;
113
+ if ((j - i) % 3 == 1) return false;
114
+ if ((j - i) % 3 == 2 && ++cnt2 > 1) return false;
115
+ i = j;
116
+ }
117
+ return cnt2 == 1;
118
+ }
119
+ };
120
+ ```
66
121
122
+ ### **Go**
123
+
124
+ ```go
125
+ func isDecomposable(s string) bool {
126
+ i, n := 0, len(s)
127
+ cnt2 := 0
128
+ for i < n {
129
+ j := i
130
+ for j < n && s[j] == s[i] {
131
+ j++
132
+ }
133
+ if (j-i)%3 == 1 {
134
+ return false
135
+ }
136
+ if (j-i)%3 == 2 {
137
+ cnt2++
138
+ if cnt2 > 1 {
139
+ return false
140
+ }
141
+ }
142
+ i = j
143
+ }
144
+ return cnt2 == 1
145
+ }
67
146
```
68
147
69
148
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ bool isDecomposable (string s) {
4
+ int i = 0 , n = s.size ();
5
+ int cnt2 = 0 ;
6
+ while (i < n) {
7
+ int j = i;
8
+ while (j < n && s[j] == s[i]) ++j;
9
+ if ((j - i) % 3 == 1 ) return false ;
10
+ if ((j - i) % 3 == 2 && ++cnt2 > 1 ) return false ;
11
+ i = j;
12
+ }
13
+ return cnt2 == 1 ;
14
+ }
15
+ };
Original file line number Diff line number Diff line change
1
+ func isDecomposable (s string ) bool {
2
+ i , n := 0 , len (s )
3
+ cnt2 := 0
4
+ for i < n {
5
+ j := i
6
+ for j < n && s [j ] == s [i ] {
7
+ j ++
8
+ }
9
+ if (j - i )% 3 == 1 {
10
+ return false
11
+ }
12
+ if (j - i )% 3 == 2 {
13
+ cnt2 ++
14
+ if cnt2 > 1 {
15
+ return false
16
+ }
17
+ }
18
+ i = j
19
+ }
20
+ return cnt2 == 1
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isDecomposable (String s ) {
3
+ int i = 0 , n = s .length ();
4
+ int cnt2 = 0 ;
5
+ while (i < n ) {
6
+ int j = i ;
7
+ while (j < n && s .charAt (j ) == s .charAt (i )) {
8
+ ++j ;
9
+ }
10
+ if ((j - i ) % 3 == 1 ) {
11
+ return false ;
12
+ }
13
+ if ((j - i ) % 3 == 2 && ++cnt2 > 1 ) {
14
+ return false ;
15
+ }
16
+ i = j ;
17
+ }
18
+ return cnt2 == 1 ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isDecomposable (self , s : str ) -> bool :
3
+ i , n = 0 , len (s )
4
+ cnt2 = 0
5
+ while i < n :
6
+ j = i
7
+ while j < n and s [j ] == s [i ]:
8
+ j += 1
9
+ if (j - i ) % 3 == 1 :
10
+ return False
11
+ cnt2 += (j - i ) % 3 == 2
12
+ if cnt2 > 1 :
13
+ return False
14
+ i = j
15
+ return cnt2 == 1
You can’t perform that action at this time.
0 commit comments