48
48
49
49
<!-- 这里可写通用的实现逻辑 -->
50
50
51
- 用一个数组或字典 chars 存放 magazine 中每个字母出现的次数。遍历 ransomNote 中每个字母,判断 chars 是否包含即可。
51
+ ** 方法一:哈希表或数组**
52
+
53
+ 我们可以用一个哈希表或长度为 $26$ 的数组 $cnt$ 记录字符串 ` magazine ` 中所有字符出现的次数。然后遍历字符串 ` ransomNote ` ,对于其中的每个字符 $c$,我们将其从 $cnt$ 的次数减 $1$,如果减 $1$ 之后的次数小于 $0$,说明 $c$ 在 ` magazine ` 中出现的次数不够,因此无法构成 ` ransomNote ` ,返回 $false$ 即可。
54
+
55
+ 否则,遍历结束后,说明 ` ransomNote ` 中的每个字符都可以在 ` magazine ` 中找到对应的字符,因此返回 $true$。
56
+
57
+ 时间复杂度 $O(m + n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别为字符串 ` ransomNote ` 和 ` magazine ` 的长度;而 $C$ 为字符集的大小,本题中 $C = 26$。
52
58
53
59
<!-- tabs:start -->
54
60
59
65
``` python
60
66
class Solution :
61
67
def canConstruct (self , ransomNote : str , magazine : str ) -> bool :
62
- counter = Counter(magazine)
68
+ cnt = Counter(magazine)
63
69
for c in ransomNote:
64
- if counter[c] <= 0 :
70
+ cnt[c] -= 1
71
+ if cnt[c] < 0 :
65
72
return False
66
- counter[c] -= 1
67
73
return True
68
74
```
69
75
@@ -74,50 +80,34 @@ class Solution:
74
80
``` java
75
81
class Solution {
76
82
public boolean canConstruct (String ransomNote , String magazine ) {
77
- int [] counter = new int [26 ];
78
- for (char c : magazine. toCharArray() ) {
79
- ++ counter[c - ' a' ];
83
+ int [] cnt = new int [26 ];
84
+ for (int i = 0 ; i < magazine. length(); ++ i ) {
85
+ ++ cnt[magazine . charAt(i) - ' a' ];
80
86
}
81
- for (char c : ransomNote. toCharArray() ) {
82
- if (counter[c - ' a' ] <= 0 ) {
87
+ for (int i = 0 ; i < ransomNote. length(); ++ i ) {
88
+ if (-- cnt[ransomNote . charAt(i) - ' a' ] < 0 ) {
83
89
return false ;
84
90
}
85
- -- counter[c - ' a' ];
86
91
}
87
92
return true ;
88
93
}
89
94
}
90
95
```
91
96
92
- ### ** TypeScript**
93
-
94
- ``` ts
95
- function canConstruct(ransomNote : string , magazine : string ): boolean {
96
- let counter = new Array (26 ).fill (0 );
97
- let base = ' a' .charCodeAt (0 );
98
- for (let s of magazine ) {
99
- ++ counter [s .charCodeAt (0 ) - base ];
100
- }
101
- for (let s of ransomNote ) {
102
- let idx = s .charCodeAt (0 ) - base ;
103
- if (counter [idx ] == 0 ) return false ;
104
- -- counter [idx ];
105
- }
106
- return true ;
107
- }
108
- ```
109
-
110
97
### ** C++**
111
98
112
99
``` cpp
113
100
class Solution {
114
101
public:
115
102
bool canConstruct(string ransomNote, string magazine) {
116
- vector<int > counter(26);
117
- for (char c : magazine) ++counter[ c - 'a'] ;
118
- for (char c : ransomNote) {
119
- if (counter[ c - 'a'] <= 0) return false;
120
- --counter[ c - 'a'] ;
103
+ int cnt[ 26] {};
104
+ for (char& c : magazine) {
105
+ ++cnt[ c - 'a'] ;
106
+ }
107
+ for (char& c : ransomNote) {
108
+ if (--cnt[ c - 'a'] < 0) {
109
+ return false;
110
+ }
121
111
}
122
112
return true;
123
113
}
@@ -128,25 +118,37 @@ public:
128
118
129
119
```go
130
120
func canConstruct(ransomNote string, magazine string) bool {
131
- rc := make([]int, 26)
132
- for _, b := range ransomNote {
133
- rc[b-'a']++
134
- }
135
-
136
- mc := make([]int, 26)
137
- for _, b := range magazine {
138
- mc[b-'a']++
121
+ cnt := [26]int{}
122
+ for _, c := range magazine {
123
+ cnt[c-'a']++
139
124
}
140
-
141
- for i := 0; i < 26; i++ {
142
- if rc[i] > mc[i] {
125
+ for _, c := range ransomNote {
126
+ cnt[c-'a']--
127
+ if cnt[c-'a'] < 0 {
143
128
return false
144
129
}
145
130
}
146
131
return true
147
132
}
148
133
```
149
134
135
+ ### ** TypeScript**
136
+
137
+ ``` ts
138
+ function canConstruct(ransomNote : string , magazine : string ): boolean {
139
+ const cnt = new Array (26 ).fill (0 );
140
+ for (const c of magazine ) {
141
+ ++ cnt [c .charCodeAt (0 ) - 97 ];
142
+ }
143
+ for (const c of ransomNote ) {
144
+ if (-- cnt [c .charCodeAt (0 ) - 97 ] < 0 ) {
145
+ return false ;
146
+ }
147
+ }
148
+ return true ;
149
+ }
150
+ ```
151
+
150
152
### ** PHP**
151
153
152
154
``` php
0 commit comments