Skip to content

Commit

Permalink
[leetcode-2287] Rearrange Characters to Make Target String
Browse files Browse the repository at this point in the history
Change-Id: Idb9034b7af41abf65cf7643deef1085397ec86df
Signed-off-by: carlos <carlos.wei.hk@gmail.com>
  • Loading branch information
carloscn committed Oct 10, 2023
1 parent 36bba9e commit 3ba4dc1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ C语言无法像是高级计算机语言对基础数据结构有一部分的实
* [leetcode2269: Find the K-Beauty of a Number](https://github.com/carloscn/structstudy/issues/371) [2023-10-07]
* [leetcode2273: Find Resultant Array After Removing Anagrams](https://github.com/carloscn/structstudy/issues/372) [2023-10-09]
* [leetcode2278: Percentage of Letter in String](https://github.com/carloscn/structstudy/issues/373) [2023-10-09]
* [leetcode2287: Rearrange Characters to Make Target String](https://github.com/carloscn/structstudy/issues/375) [2023-10-10]
## [链表](https://github.com/carloscn/structstudy/tree/master/c_programming/linklist)
* [删除链表的节点](https://github.com/carloscn/structstudy/issues/15)
* [反转链表(leetcode-206)reverse-linked-list](https://github.com/carloscn/structstudy/issues/31) [2022-10-14]
Expand Down
1 change: 1 addition & 0 deletions rust_programming/str/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ mod n122_minimum_number_of_operations_to_convert_time_2224;
mod n123_calculate_digit_sum_of_a_string_2243;
mod n124_largest_3_same_digit_number_in_string_2264;
mod n125_find_resultant_array_after_removing_anagrams_2273;
mod n126_rearrange_characters_to_make_target_string_2287;
fn main() {
println!("hello leetcode string project!");
}
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);
}
}

0 comments on commit 3ba4dc1

Please sign in to comment.