-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[leetcode-2287] Rearrange Characters to Make Target String
Change-Id: Idb9034b7af41abf65cf7643deef1085397ec86df Signed-off-by: carlos <carlos.wei.hk@gmail.com>
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
rust_programming/str/src/n126_rearrange_characters_to_make_target_string_2287.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
pub fn rearrange_characters(s: &str, target: &str) -> i32 | ||
{ | ||
let mut ret:i32 = 0; | ||
if s.is_empty() || target.is_empty() || s.len() < target.len() { | ||
return ret; | ||
} | ||
|
||
let mut s_dup:Vec<char> = s.chars().collect(); | ||
let t_dup:Vec<char> = target.chars().collect(); | ||
let mut count_min_number:usize = usize::MAX; | ||
for i in 0..target.len() { | ||
let mut j:usize = 0; | ||
let mut count:usize = 0; | ||
let mut index:usize = 0; | ||
while j < s_dup.len() { | ||
if s_dup[j] == t_dup[i] { | ||
count += 1; | ||
index = j; | ||
} | ||
j += 1; | ||
} | ||
s_dup.remove(index); | ||
count_min_number = count.min(count_min_number); | ||
} | ||
|
||
ret = count_min_number as i32; | ||
|
||
return ret; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn demo() { | ||
let ret = rearrange_characters("ilovecodingonleetcode", "code"); | ||
assert_eq!(ret, 2); | ||
let ret = rearrange_characters("abcba", "abc"); | ||
assert_eq!(ret, 1); | ||
let ret = rearrange_characters("abbaccaddaeea", "aaaaa"); | ||
assert_eq!(ret, 1); | ||
} | ||
} |