Skip to content

Commit

Permalink
add: 最大字符串配对数目
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Jan 17, 2024
1 parent eac7d80 commit 1e8d539
Show file tree
Hide file tree
Showing 6 changed files with 64 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/find_maximum_number_of_string_pairs.rs) [数组, 哈希表, 字符串, 模拟]

- LeetCode 2744. 最大字符串配对数目 <https://leetcode.cn/problems/find-maximum-number-of-string-pairs>

- [统计出现过一次的公共字符串](src/array/count_common_words_with_one_occurrence.rs) [数组, 哈希表, 字符串, 计数]

- LeetCode 2085. 统计出现过一次的公共字符串 <https://leetcode.cn/problems/count-common-words-with-one-occurrence>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/array/find_maximum_number_of_string_pairs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 最大字符串配对数目
// https://leetcode.cn/problems/find-maximum-number-of-string-pairs
// INLINE ../../images/array/find_maximum_number_of_string_pairs.jpeg

pub struct Solution;

impl Solution {
pub fn maximum_number_of_string_pairs(words: Vec<String>) -> i32 {
let n = words.len();
let mut ans = 0;
for i in 0..n {
for j in (i + 1)..n {
let word_i = words[i].as_bytes();
let word_j = words[j].as_bytes();
if word_i[0] == word_j[1] && word_i[1] == word_j[0] {
ans += 1;
}
}
}
ans
}
}
1 change: 1 addition & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ pub mod min_cost_climbing_stairs;
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;
36 changes: 36 additions & 0 deletions tests/array/find_maximum_number_of_string_pairs_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use rust_practice::array::find_maximum_number_of_string_pairs::Solution;

#[test]
fn maximum_number_of_string_pairs_test() {
// 示例 1:
// 输入:words = ["cd","ac","dc","ca","zz"]
// 输出:2
// 解释:在此示例中,我们可以通过以下方式匹配 2 对字符串:
// - 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。
// - 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。
// 可以证明最多匹配数目是 2 。
let words = vec![
"cd".to_string(),
"ac".to_string(),
"dc".to_string(),
"ca".to_string(),
"zz".to_string(),
];
assert_eq!(Solution::maximum_number_of_string_pairs(words), 2);

// 示例 2:
// 输入:words = ["ab","ba","cc"]
// 输出:1
// 解释:在此示例中,我们可以通过以下方式匹配 1 对字符串:
// - 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。
// 可以证明最多匹配数目是 1 。
let words = vec!["ab".to_string(), "ba".to_string(), "cc".to_string()];
assert_eq!(Solution::maximum_number_of_string_pairs(words), 1);

// 示例 3:
// 输入:words = ["aa","ab"]
// 输出:0
// 解释:这个例子中,无法匹配任何字符串。
let words = vec!["aa".to_string(), "ab".to_string()];
assert_eq!(Solution::maximum_number_of_string_pairs(words), 0);
}
1 change: 1 addition & 0 deletions tests/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ pub mod teemo_attacking_test;
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;

0 comments on commit 1e8d539

Please sign in to comment.