-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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 | ||
} | ||
} |
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
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); | ||
} |
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