20231220 Finished

This commit is contained in:
jackfiled 2023-12-20 10:42:38 +08:00
parent d0dddf4ca6
commit 1c39bcee9f
2 changed files with 79 additions and 0 deletions

View File

@ -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;

View File

@ -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.
*
* <strong class="example">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.
*
* <strong class="example">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.
*
* <strong class="example">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<String>, 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")));
}
}