Skip to content

Commit 1f7c6db

Browse files
committed
added solution in rust: 383. Ransom Note
Signed-off-by: rajput-hemant <[email protected]>
1 parent c0ac862 commit 1f7c6db

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)