File tree 6 files changed +235
-2
lines changed
solution/1500-1599/1525.Number of Good Ways to Split a String
6 files changed +235
-2
lines changed Original file line number Diff line number Diff line change 56
56
57
57
<!-- 这里可写通用的实现逻辑 -->
58
58
59
+ ** 方法一:哈希表**
60
+
61
+ 我们维护两个哈希表,分别记录左侧出现的字符、右侧的字符以及出现的次数。初始时,左侧的哈希表为空,右侧的哈希表为字符串 $s$ 中所有字符出现的次数。
62
+
63
+ 接下来,我们从左到右遍历字符串 $s$,对于遍历到的字符 $c$,我们将其加入左侧的哈希表,同时将其在右侧的哈希表中的出现次数减一。如果减一后,右侧哈希表中的出现次数为 0,则将其从右侧哈希表中移除。然后,我们判断左侧哈希表中的键值对数量是否与右侧哈希表中的键值对数量相等,如果相等,则将答案加一。
64
+
65
+ 最终,我们返回答案即可。
66
+
67
+ 时间复杂度为 $O(n)$,空间复杂度为 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
68
+
59
69
<!-- tabs:start -->
60
70
61
71
### ** Python3**
62
72
63
73
<!-- 这里可写当前语言的特殊实现逻辑 -->
64
74
65
75
``` python
66
-
76
+ class Solution :
77
+ def numSplits (self , s : str ) -> int :
78
+ cnt = Counter(s)
79
+ vis = set ()
80
+ ans = 0
81
+ for c in s:
82
+ vis.add(c)
83
+ cnt[c] -= 1
84
+ if cnt[c] == 0 :
85
+ cnt.pop(c)
86
+ ans += len (vis) == len (cnt)
87
+ return ans
67
88
```
68
89
69
90
### ** Java**
70
91
71
92
<!-- 这里可写当前语言的特殊实现逻辑 -->
72
93
73
94
``` java
95
+ class Solution {
96
+ public int numSplits (String s ) {
97
+ Map<Character , Integer > cnt = new HashMap<> ();
98
+ for (char c : s. toCharArray()) {
99
+ cnt. merge(c, 1 , Integer :: sum);
100
+ }
101
+ Set<Character > vis = new HashSet<> ();
102
+ int ans = 0 ;
103
+ for (char c : s. toCharArray()) {
104
+ vis. add(c);
105
+ if (cnt. merge(c, - 1 , Integer :: sum) == 0 ) {
106
+ cnt. remove(c);
107
+ }
108
+ if (vis. size() == cnt. size()) {
109
+ ++ ans;
110
+ }
111
+ }
112
+ return ans;
113
+ }
114
+ }
115
+ ```
116
+
117
+ ### ** C++**
118
+
119
+ ``` cpp
120
+ class Solution {
121
+ public:
122
+ int numSplits(string s) {
123
+ unordered_map<char, int> cnt;
124
+ for (char& c : s) {
125
+ ++cnt[ c] ;
126
+ }
127
+ unordered_set<char > vis;
128
+ int ans = 0;
129
+ for (char& c : s) {
130
+ vis.insert(c);
131
+ if (--cnt[ c] == 0) {
132
+ cnt.erase(c);
133
+ }
134
+ ans += vis.size() == cnt.size();
135
+ }
136
+ return ans;
137
+ }
138
+ };
139
+ ```
74
140
141
+ ### **Go**
142
+
143
+ ```go
144
+ func numSplits(s string) (ans int) {
145
+ cnt := map[rune]int{}
146
+ for _, c := range s {
147
+ cnt[c]++
148
+ }
149
+ vis := map[rune]bool{}
150
+ for _, c := range s {
151
+ vis[c] = true
152
+ cnt[c]--
153
+ if cnt[c] == 0 {
154
+ delete(cnt, c)
155
+ }
156
+ if len(vis) == len(cnt) {
157
+ ans++
158
+ }
159
+ }
160
+ return
161
+ }
75
162
```
76
163
77
164
### ** ...**
Original file line number Diff line number Diff line change 47
47
### ** Python3**
48
48
49
49
``` python
50
-
50
+ class Solution :
51
+ def numSplits (self , s : str ) -> int :
52
+ cnt = Counter(s)
53
+ vis = set ()
54
+ ans = 0
55
+ for c in s:
56
+ vis.add(c)
57
+ cnt[c] -= 1
58
+ if cnt[c] == 0 :
59
+ cnt.pop(c)
60
+ ans += len (vis) == len (cnt)
61
+ return ans
51
62
```
52
63
53
64
### ** Java**
54
65
55
66
``` java
67
+ class Solution {
68
+ public int numSplits (String s ) {
69
+ Map<Character , Integer > cnt = new HashMap<> ();
70
+ for (char c : s. toCharArray()) {
71
+ cnt. merge(c, 1 , Integer :: sum);
72
+ }
73
+ Set<Character > vis = new HashSet<> ();
74
+ int ans = 0 ;
75
+ for (char c : s. toCharArray()) {
76
+ vis. add(c);
77
+ if (cnt. merge(c, - 1 , Integer :: sum) == 0 ) {
78
+ cnt. remove(c);
79
+ }
80
+ if (vis. size() == cnt. size()) {
81
+ ++ ans;
82
+ }
83
+ }
84
+ return ans;
85
+ }
86
+ }
87
+ ```
88
+
89
+ ### ** C++**
90
+
91
+ ``` cpp
92
+ class Solution {
93
+ public:
94
+ int numSplits(string s) {
95
+ unordered_map<char, int> cnt;
96
+ for (char& c : s) {
97
+ ++cnt[ c] ;
98
+ }
99
+ unordered_set<char > vis;
100
+ int ans = 0;
101
+ for (char& c : s) {
102
+ vis.insert(c);
103
+ if (--cnt[ c] == 0) {
104
+ cnt.erase(c);
105
+ }
106
+ ans += vis.size() == cnt.size();
107
+ }
108
+ return ans;
109
+ }
110
+ };
111
+ ```
56
112
113
+ ### **Go**
114
+
115
+ ```go
116
+ func numSplits(s string) (ans int) {
117
+ cnt := map[rune]int{}
118
+ for _, c := range s {
119
+ cnt[c]++
120
+ }
121
+ vis := map[rune]bool{}
122
+ for _, c := range s {
123
+ vis[c] = true
124
+ cnt[c]--
125
+ if cnt[c] == 0 {
126
+ delete(cnt, c)
127
+ }
128
+ if len(vis) == len(cnt) {
129
+ ans++
130
+ }
131
+ }
132
+ return
133
+ }
57
134
```
58
135
59
136
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numSplits (string s) {
4
+ unordered_map<char , int > cnt;
5
+ for (char & c : s) {
6
+ ++cnt[c];
7
+ }
8
+ unordered_set<char > vis;
9
+ int ans = 0 ;
10
+ for (char & c : s) {
11
+ vis.insert (c);
12
+ if (--cnt[c] == 0 ) {
13
+ cnt.erase (c);
14
+ }
15
+ ans += vis.size () == cnt.size ();
16
+ }
17
+ return ans;
18
+ }
19
+ };
Original file line number Diff line number Diff line change
1
+ func numSplits (s string ) (ans int ) {
2
+ cnt := map [rune ]int {}
3
+ for _ , c := range s {
4
+ cnt [c ]++
5
+ }
6
+ vis := map [rune ]bool {}
7
+ for _ , c := range s {
8
+ vis [c ] = true
9
+ cnt [c ]--
10
+ if cnt [c ] == 0 {
11
+ delete (cnt , c )
12
+ }
13
+ if len (vis ) == len (cnt ) {
14
+ ans ++
15
+ }
16
+ }
17
+ return
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numSplits (String s ) {
3
+ Map <Character , Integer > cnt = new HashMap <>();
4
+ for (char c : s .toCharArray ()) {
5
+ cnt .merge (c , 1 , Integer ::sum );
6
+ }
7
+ Set <Character > vis = new HashSet <>();
8
+ int ans = 0 ;
9
+ for (char c : s .toCharArray ()) {
10
+ vis .add (c );
11
+ if (cnt .merge (c , -1 , Integer ::sum ) == 0 ) {
12
+ cnt .remove (c );
13
+ }
14
+ if (vis .size () == cnt .size ()) {
15
+ ++ans ;
16
+ }
17
+ }
18
+ return ans ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numSplits (self , s : str ) -> int :
3
+ cnt = Counter (s )
4
+ vis = set ()
5
+ ans = 0
6
+ for c in s :
7
+ vis .add (c )
8
+ cnt [c ] -= 1
9
+ if cnt [c ] == 0 :
10
+ cnt .pop (c )
11
+ ans += len (vis ) == len (cnt )
12
+ return ans
You can’t perform that action at this time.
0 commit comments