From 1c39bcee9f8f30f359ef98f46431e89a85392c03 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 20 Dec 2023 10:42:38 +0800 Subject: [PATCH] 20231220 Finished --- src/solution/mod.rs | 1 + ...heck_if_a_string_is_an_acronym_of_words.rs | 78 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/solution/s2828_check_if_a_string_is_an_acronym_of_words.rs diff --git a/src/solution/mod.rs b/src/solution/mod.rs index 5e2b42e..bf37cc0 100644 --- a/src/solution/mod.rs +++ b/src/solution/mod.rs @@ -4,3 +4,4 @@ mod s0020_valid_parentheses; mod s2697_lexicographically_smallest_palindrome; mod s0002_add_two_numbers; mod s0003_longest_substring_without_repeating_characters; +mod s2828_check_if_a_string_is_an_acronym_of_words; diff --git a/src/solution/s2828_check_if_a_string_is_an_acronym_of_words.rs b/src/solution/s2828_check_if_a_string_is_an_acronym_of_words.rs new file mode 100644 index 0000000..eaecc18 --- /dev/null +++ b/src/solution/s2828_check_if_a_string_is_an_acronym_of_words.rs @@ -0,0 +1,78 @@ +/** + * [2828] Check if a String Is an Acronym of Words + * + * Given an array of strings words and a string s, determine if s is an acronym of words. + * The string s is considered an acronym of words if it can be formed by concatenating the first character of each string in words in order. For example, "ab" can be formed from ["apple", "banana"], but it can't be formed from ["bear", "aardvark"]. + * Return true if s is an acronym of words, and false otherwise. + * + * Example 1: + * + * Input: words = ["alice","bob","charlie"], s = "abc" + * Output: true + * Explanation: The first character in the words "alice", "bob", and "charlie" are 'a', 'b', and 'c', respectively. Hence, s = "abc" is the acronym. + * + * Example 2: + * + * Input: words = ["an","apple"], s = "a" + * Output: false + * Explanation: The first character in the words "an" and "apple" are 'a' and 'a', respectively. + * The acronym formed by concatenating these characters is "aa". + * Hence, s = "a" is not the acronym. + * + * Example 3: + * + * Input: words = ["never","gonna","give","up","on","you"], s = "ngguoy" + * Output: true + * Explanation: By concatenating the first character of the words in the array, we get the string "ngguoy". + * Hence, s = "ngguoy" is the acronym. + * + * + * Constraints: + * + * 1 <= words.length <= 100 + * 1 <= words[i].length <= 10 + * 1 <= s.length <= 100 + * words[i] and s consist of lowercase English letters. + * + */ +pub struct Solution {} + +// problem: https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/ +// discuss: https://leetcode.cn/problems/check-if-a-string-is-an-acronym-of-words/discuss/?currentPage=1&orderBy=most_votes&query= + +// submission codes start here + +impl Solution { + pub fn is_acronym(words: Vec, s: String) -> bool { + if words.len() != s.len() { + return false; + } + + for (index, value) in s.chars().enumerate() { + if !words[index].starts_with(value) { + return false; + } + } + + true + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_2828() { + assert_eq!(true, Solution::is_acronym(vec![ + String::from("alice"), + String::from("bob"), + String::from("charlie")], String::from("abc"))); + + assert_eq!(false, Solution::is_acronym(vec![ + String::from("an"), + String::from("apple")], String::from("a"))); + } +}