We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c0ac862 commit 1f7c6dbCopy full SHA for 1f7c6db
src/0301-0400/383 - Ransom Note/ransom_note.rs
@@ -0,0 +1,21 @@
1
+impl Solution {
2
+ pub fn can_construct(ransom_note: String, magazine: String) -> bool {
3
+ // Convert the magazine string to a vector of characters
4
+ let mut magazine = magazine.chars().collect::<Vec<char>>();
5
+
6
+ // Iterate over the ransom note string
7
+ for c in ransom_note.chars() {
8
+ // If the magazine contains the character, remove it
9
+ // Otherwise, return false
10
+ // position() returns the index of the first element that matches the closure
11
+ // else returns None
12
+ if let Some(i) = magazine.iter().position(|&x| x == c) {
13
+ magazine.remove(i);
14
+ } else {
15
+ return false;
16
+ }
17
18
19
+ true
20
21
+}
0 commit comments