|
1 | | -# 383. Ransom Note - Best Practices of LeetCode Solutions |
| 1 | +# 383. Ransom Note - Fucking Good LeetCode Solutions |
| 2 | + |
2 | 3 | LeetCode link: [383. Ransom Note](https://leetcode.com/problems/ransom-note), |
3 | 4 | [383. 赎金信](https://leetcode.cn/problems/ransom-note) |
4 | 5 |
|
@@ -256,3 +257,104 @@ for (character in ransomNote) { |
256 | 257 |
|
257 | 258 | return true |
258 | 259 | ``` |
| 260 | + |
| 261 | +## 复杂度 |
| 262 | +* 时间:`O(n)`。 |
| 263 | +* 空间:`O(n)`。 |
| 264 | + |
| 265 | +## Java |
| 266 | +```java |
| 267 | +class Solution { |
| 268 | + public boolean canConstruct(String ransomNote, String magazine) { |
| 269 | + var charToCount = new HashMap<Character, Integer>(); |
| 270 | + |
| 271 | + for (var character : magazine.toCharArray()) { |
| 272 | + charToCount.put(character, charToCount.getOrDefault(character, 0) + 1); |
| 273 | + } |
| 274 | + |
| 275 | + for (var character : ransomNote.toCharArray()) { |
| 276 | + charToCount.put(character, charToCount.getOrDefault(character, 0) - 1); |
| 277 | + |
| 278 | + if (charToCount.get(character) < 0) { |
| 279 | + return false; |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return true; |
| 284 | + } |
| 285 | +} |
| 286 | +``` |
| 287 | + |
| 288 | +## Python |
| 289 | +```python |
| 290 | +# from collections import defaultdict |
| 291 | + |
| 292 | +class Solution: |
| 293 | + def canConstruct(self, ransomNote: str, magazine: str) -> bool: |
| 294 | + char_to_count = defaultdict(int) |
| 295 | + |
| 296 | + for char in magazine: |
| 297 | + char_to_count[char] += 1 |
| 298 | + |
| 299 | + for char in ransomNote: |
| 300 | + char_to_count[char] -= 1 |
| 301 | + |
| 302 | + if char_to_count[char] < 0: |
| 303 | + return False |
| 304 | + |
| 305 | + return True |
| 306 | +``` |
| 307 | + |
| 308 | +## JavaScript |
| 309 | +```javascript |
| 310 | +var canConstruct = function (ransomNote, magazine) { |
| 311 | + const charToCount = new Map() |
| 312 | + |
| 313 | + for (const character of magazine) { |
| 314 | + charToCount.set(character, (charToCount.get(character) || 0) + 1) |
| 315 | + } |
| 316 | + |
| 317 | + for (const character of ransomNote) { |
| 318 | + charToCount.set(character, (charToCount.get(character) || 0) - 1) |
| 319 | + |
| 320 | + if (charToCount.get(character) < 0) { |
| 321 | + return false |
| 322 | + } |
| 323 | + } |
| 324 | + |
| 325 | + return true |
| 326 | +}; |
| 327 | +``` |
| 328 | + |
| 329 | +## C# |
| 330 | +```c# |
| 331 | +public class Solution |
| 332 | +{ |
| 333 | + public bool CanConstruct(string ransomNote, string magazine) |
| 334 | + { |
| 335 | + var charToCount = new Dictionary<char, int>(); |
| 336 | + |
| 337 | + foreach (char character in magazine) |
| 338 | + charToCount[character] = charToCount.GetValueOrDefault(character, 0) + 1; |
| 339 | + |
| 340 | + foreach (char character in ransomNote) |
| 341 | + { |
| 342 | + charToCount[character] = charToCount.GetValueOrDefault(character, 0) - 1; |
| 343 | + |
| 344 | + if (charToCount[character] < 0) |
| 345 | + { |
| 346 | + return false; |
| 347 | + } |
| 348 | + } |
| 349 | + |
| 350 | + return true; |
| 351 | + } |
| 352 | +} |
| 353 | +``` |
| 354 | + |
| 355 | +## Other languages |
| 356 | +```java |
| 357 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 358 | +``` |
| 359 | + |
| 360 | +原文链接:[leetcoder.net - Fucking Good LeetCode Solutions](https://leetcoder.net/en/leetcode/383-ransom-note) |
0 commit comments