File tree 6 files changed +168
-66
lines changed
solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences
6 files changed +168
-66
lines changed Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
+ ** 方法一:计数**
44
+
45
+ 我们用一个哈希表或一个长度为 $26$ 的数组 $cnt$ 记录字符串 $s$ 中每个字符出现的次数。
46
+
47
+ 接下来遍历 $cnt$ 中的每个值,判断所有非零值是否相等即可。
48
+
49
+ 时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 $s$ 的长度;而 $C$ 是字符集大小,本题中字符集为小写英文字母,因此 $C=26$。
50
+
43
51
<!-- tabs:start -->
44
52
45
53
### ** Python3**
@@ -61,18 +69,17 @@ class Solution:
61
69
class Solution {
62
70
public boolean areOccurrencesEqual (String s ) {
63
71
int [] cnt = new int [26 ];
64
- for (char c : s . toCharArray() ) {
65
- ++ cnt[c - ' a' ];
72
+ for (int i = 0 ; i < s . length(); ++ i ) {
73
+ ++ cnt[s . charAt(i) - ' a' ];
66
74
}
67
- int t = 0 ;
75
+ int x = 0 ;
68
76
for (int v : cnt) {
69
- if (v == 0 ) {
70
- continue ;
71
- }
72
- if (t == 0 ) {
73
- t = v;
74
- } else if (t != v) {
75
- return false ;
77
+ if (v > 0 ) {
78
+ if (x == 0 ) {
79
+ x = v;
80
+ } else if (x != v) {
81
+ return false ;
82
+ }
76
83
}
77
84
}
78
85
return true ;
@@ -86,12 +93,21 @@ class Solution {
86
93
class Solution {
87
94
public:
88
95
bool areOccurrencesEqual(string s) {
89
- vector<int > cnt(26);
90
- for (char& c : s) ++cnt[ c - 'a'] ;
91
- unordered_set<int > ss;
92
- for (int& v : cnt)
93
- if (v) ss.insert(v);
94
- return ss.size() == 1;
96
+ int cnt[ 26] {};
97
+ for (char& c : s) {
98
+ ++cnt[ c - 'a'] ;
99
+ }
100
+ int x = 0;
101
+ for (int& v : cnt) {
102
+ if (v) {
103
+ if (!x) {
104
+ x = v;
105
+ } else if (x != v) {
106
+ return false;
107
+ }
108
+ }
109
+ }
110
+ return true;
95
111
}
96
112
};
97
113
```
@@ -100,18 +116,43 @@ public:
100
116
101
117
```go
102
118
func areOccurrencesEqual(s string) bool {
103
- cnt := make([ ]int, 26)
119
+ cnt := [26 ]int{}
104
120
for _, c := range s {
105
121
cnt[c-'a']++
106
122
}
107
- ss := map[int]bool{}
123
+ x := 0
108
124
for _, v := range cnt {
109
- if v == 0 {
110
- continue
125
+ if v > 0 {
126
+ if x == 0 {
127
+ x = v
128
+ } else if x != v {
129
+ return false
130
+ }
111
131
}
112
- ss[v] = true
113
132
}
114
- return len(ss) == 1
133
+ return true
134
+ }
135
+ ```
136
+
137
+ ### ** TypeScript**
138
+
139
+ ``` ts
140
+ function areOccurrencesEqual(s : string ): boolean {
141
+ const cnt: number [] = new Array (26 ).fill (0 );
142
+ for (const c of s ) {
143
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
144
+ }
145
+ let x = 0 ;
146
+ for (const v of cnt ) {
147
+ if (v ) {
148
+ if (! x ) {
149
+ x = v ;
150
+ } else if (x !== v ) {
151
+ return false ;
152
+ }
153
+ }
154
+ }
155
+ return true ;
115
156
}
116
157
```
117
158
Original file line number Diff line number Diff line change @@ -53,18 +53,17 @@ class Solution:
53
53
class Solution {
54
54
public boolean areOccurrencesEqual (String s ) {
55
55
int [] cnt = new int [26 ];
56
- for (char c : s . toCharArray() ) {
57
- ++ cnt[c - ' a' ];
56
+ for (int i = 0 ; i < s . length(); ++ i ) {
57
+ ++ cnt[s . charAt(i) - ' a' ];
58
58
}
59
- int t = 0 ;
59
+ int x = 0 ;
60
60
for (int v : cnt) {
61
- if (v == 0 ) {
62
- continue ;
63
- }
64
- if (t == 0 ) {
65
- t = v;
66
- } else if (t != v) {
67
- return false ;
61
+ if (v > 0 ) {
62
+ if (x == 0 ) {
63
+ x = v;
64
+ } else if (x != v) {
65
+ return false ;
66
+ }
68
67
}
69
68
}
70
69
return true ;
@@ -78,12 +77,21 @@ class Solution {
78
77
class Solution {
79
78
public:
80
79
bool areOccurrencesEqual(string s) {
81
- vector<int > cnt(26);
82
- for (char& c : s) ++cnt[ c - 'a'] ;
83
- unordered_set<int > ss;
84
- for (int& v : cnt)
85
- if (v) ss.insert(v);
86
- return ss.size() == 1;
80
+ int cnt[ 26] {};
81
+ for (char& c : s) {
82
+ ++cnt[ c - 'a'] ;
83
+ }
84
+ int x = 0;
85
+ for (int& v : cnt) {
86
+ if (v) {
87
+ if (!x) {
88
+ x = v;
89
+ } else if (x != v) {
90
+ return false;
91
+ }
92
+ }
93
+ }
94
+ return true;
87
95
}
88
96
};
89
97
```
@@ -92,18 +100,43 @@ public:
92
100
93
101
```go
94
102
func areOccurrencesEqual(s string) bool {
95
- cnt := make([ ]int, 26)
103
+ cnt := [26 ]int{}
96
104
for _, c := range s {
97
105
cnt[c-'a']++
98
106
}
99
- ss := map[int]bool{}
107
+ x := 0
100
108
for _, v := range cnt {
101
- if v == 0 {
102
- continue
109
+ if v > 0 {
110
+ if x == 0 {
111
+ x = v
112
+ } else if x != v {
113
+ return false
114
+ }
103
115
}
104
- ss[v] = true
105
116
}
106
- return len(ss) == 1
117
+ return true
118
+ }
119
+ ```
120
+
121
+ ### ** TypeScript**
122
+
123
+ ``` ts
124
+ function areOccurrencesEqual(s : string ): boolean {
125
+ const cnt: number [] = new Array (26 ).fill (0 );
126
+ for (const c of s ) {
127
+ ++ cnt [c .charCodeAt (0 ) - ' a' .charCodeAt (0 )];
128
+ }
129
+ let x = 0 ;
130
+ for (const v of cnt ) {
131
+ if (v ) {
132
+ if (! x ) {
133
+ x = v ;
134
+ } else if (x !== v ) {
135
+ return false ;
136
+ }
137
+ }
138
+ }
139
+ return true ;
107
140
}
108
141
```
109
142
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
bool areOccurrencesEqual (string s) {
4
- vector<int > cnt (26 );
5
- for (char & c : s) ++cnt[c - ' a' ];
6
- unordered_set<int > ss;
7
- for (int & v : cnt)
8
- if (v) ss.insert (v);
9
- return ss.size () == 1 ;
4
+ int cnt[26 ]{};
5
+ for (char & c : s) {
6
+ ++cnt[c - ' a' ];
7
+ }
8
+ int x = 0 ;
9
+ for (int & v : cnt) {
10
+ if (v) {
11
+ if (!x) {
12
+ x = v;
13
+ } else if (x != v) {
14
+ return false ;
15
+ }
16
+ }
17
+ }
18
+ return true ;
10
19
}
11
20
};
Original file line number Diff line number Diff line change 1
1
func areOccurrencesEqual (s string ) bool {
2
- cnt := make ([ ]int , 26 )
2
+ cnt := [ 26 ]int {}
3
3
for _ , c := range s {
4
4
cnt [c - 'a' ]++
5
5
}
6
- ss := map [ int ] bool {}
6
+ x := 0
7
7
for _ , v := range cnt {
8
- if v == 0 {
9
- continue
8
+ if v > 0 {
9
+ if x == 0 {
10
+ x = v
11
+ } else if x != v {
12
+ return false
13
+ }
10
14
}
11
- ss [v ] = true
12
15
}
13
- return len ( ss ) == 1
16
+ return true
14
17
}
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean areOccurrencesEqual (String s ) {
3
3
int [] cnt = new int [26 ];
4
- for (char c : s . toCharArray () ) {
5
- ++cnt [c - 'a' ];
4
+ for (int i = 0 ; i < s . length (); ++ i ) {
5
+ ++cnt [s . charAt ( i ) - 'a' ];
6
6
}
7
- int t = 0 ;
7
+ int x = 0 ;
8
8
for (int v : cnt ) {
9
- if (v == 0 ) {
10
- continue ;
11
- }
12
- if (t == 0 ) {
13
- t = v ;
14
- } else if (t != v ) {
15
- return false ;
9
+ if (v > 0 ) {
10
+ if (x == 0 ) {
11
+ x = v ;
12
+ } else if (x != v ) {
13
+ return false ;
14
+ }
16
15
}
17
16
}
18
17
return true ;
Original file line number Diff line number Diff line change
1
+ function areOccurrencesEqual ( s : string ) : boolean {
2
+ const cnt : number [ ] = new Array ( 26 ) . fill ( 0 ) ;
3
+ for ( const c of s ) {
4
+ ++ cnt [ c . charCodeAt ( 0 ) - 'a' . charCodeAt ( 0 ) ] ;
5
+ }
6
+ let x = 0 ;
7
+ for ( const v of cnt ) {
8
+ if ( v ) {
9
+ if ( ! x ) {
10
+ x = v ;
11
+ } else if ( x !== v ) {
12
+ return false ;
13
+ }
14
+ }
15
+ }
16
+ return true ;
17
+ }
You can’t perform that action at this time.
0 commit comments