Skip to content

Commit

Permalink
add: 按分隔符拆分字符串
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 20, 2024
1 parent a66354b commit 19604eb
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ Rust标准库`std::collections`提供了4种通用容器类型,包含一下8

### 数组/队列/集合/映射

- [按分隔符拆分字符串](src/array/split_strings_by_separator.rs) [数组, 字符串]

- LeetCode 2788. 按分隔符拆分字符串 <https://leetcode.cn/problems/split-strings-by-separator>

- [最大字符串配对数目](src/array/find_maximum_number_of_string_pairs.rs) [数组, 哈希表, 字符串, 模拟]

- LeetCode 2744. 最大字符串配对数目 <https://leetcode.cn/problems/find-maximum-number-of-string-pairs>
Expand Down
Binary file added images/array/split_strings_by_separator.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ pub mod maximum_profit_of_operating_a_centennial_wheel;
pub mod number_of_boomerangs;
pub mod count_common_words_with_one_occurrence;
pub mod find_maximum_number_of_string_pairs;
pub mod split_strings_by_separator;
20 changes: 20 additions & 0 deletions src/array/split_strings_by_separator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// 按分隔符拆分字符串
// https://leetcode.cn/problems/split-strings-by-separator
// INLINE ../../images/array/split_strings_by_separator.jpeg

pub struct Solution;

impl Solution {
pub fn split_words_by_separator(words: Vec<String>, separator: char) -> Vec<String> {
let mut res: Vec<String> = Vec::new();
for word in words {
let ss = word.split(separator);
for sub in ss {
if !sub.is_empty() {
res.push(sub.to_string());
}
}
}
res
}
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,4 @@ pub mod the_employee_that_worked_on_the_longest_task_test;
pub mod two_sum_test;
pub mod zero_matrix_lcci_test;
pub mod find_maximum_number_of_string_pairs_test;
pub mod split_strings_by_separator_test;
56 changes: 56 additions & 0 deletions tests/array/split_strings_by_separator_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use rust_practice::array::split_strings_by_separator::Solution;

#[test]
fn split_words_by_separator_test() {
// 示例 1:
// 输入:words = ["one.two.three","four.five","six"], separator = "."
// 输出:["one","two","three","four","five","six"]
// 解释:在本示例中,我们进行下述拆分:

// "one.two.three" 拆分为 "one", "two", "three"
// "four.five" 拆分为 "four", "five"
// "six" 拆分为 "six"

// 因此,结果数组为 ["one","two","three","four","five","six"] 。
let words = vec![
"one.two.three".to_string(),
"four.five".to_string(),
"six".to_string(),
];
let separator = '.';
let result = Solution::split_words_by_separator(words, separator);
assert_eq!(
result,
vec![
"one".to_string(),
"two".to_string(),
"three".to_string(),
"four".to_string(),
"five".to_string(),
"six".to_string(),
]
);

// 示例 2:
// 输入:words = ["$easy$","$problem$"], separator = "$"
// 输出:["easy","problem"]
// 解释:在本示例中,我们进行下述拆分:

// "$easy$" 拆分为 "easy"(不包括空字符串)
// "$problem$" 拆分为 "problem"(不包括空字符串)

// 因此,结果数组为 ["easy","problem"] 。
let words = vec!["$easy$".to_string(), "$problem$".to_string()];
let separator = '$';
let result = Solution::split_words_by_separator(words, separator);
assert_eq!(result, vec!["easy".to_string(), "problem".to_string()]);

// 示例 3:
// 输入:words = ["|||"], separator = "|"
// 输出:[]
// 解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。
let words = vec!["|||".to_string()];
let separator = '|';
let result = Solution::split_words_by_separator(words, separator);
assert_eq!(result.len(), 0);
}

0 comments on commit 19604eb

Please sign in to comment.