3042. Count Prefix and Suffix Pairs I Easy

@problem@discussion
#Array#String#Trie#Rolling Hash#String Matching#Hash Function



1/**
2 * [3042] Count Prefix and Suffix Pairs I
3 *
4 * You are given a 0-indexed string array words.
5 * Let's define a boolean function isPrefixAndSuffix that takes two strings, str1 and str2:
6 * 
7 * 	isPrefixAndSuffix(str1, str2) returns true if str1 is both a <span data-keyword="string-prefix">prefix</span> and a <span data-keyword="string-suffix">suffix</span> of str2, and false otherwise.
8 * 
9 * For example, isPrefixAndSuffix("aba", "ababa") is true because "aba" is a prefix of "ababa" and also a suffix, but isPrefixAndSuffix("abc", "abcd") is false.
10 * Return an integer denoting the number of index pairs (i, j) such that i < j, and isPrefixAndSuffix(words[i], words[j]) is true.
11 *  
12 * <strong class="example">Example 1:
13 * 
14 * Input: words = ["a","aba","ababa","aa"]
15 * Output: 4
16 * Explanation: In this example, the counted index pairs are:
17 * i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.
18 * i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.
19 * i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.
20 * i = 1 and j = 2 because isPrefixAndSuffix("aba", "ababa") is true.
21 * Therefore, the answer is 4.
22 * <strong class="example">Example 2:
23 * 
24 * Input: words = ["pa","papa","ma","mama"]
25 * Output: 2
26 * Explanation: In this example, the counted index pairs are:
27 * i = 0 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.
28 * i = 2 and j = 3 because isPrefixAndSuffix("ma", "mama") is true.
29 * Therefore, the answer is 2.  
30 * <strong class="example">Example 3:
31 * 
32 * Input: words = ["abab","ab"]
33 * Output: 0
34 * Explanation: In this example, the only valid index pair is i = 0 and j = 1, and isPrefixAndSuffix("abab", "ab") is false.
35 * Therefore, the answer is 0.
36 *  
37 * Constraints:
38 * 
39 * 	1 <= words.length <= 50
40 * 	1 <= words[i].length <= 10
41 * 	words[i] consists only of lowercase English letters.
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/
47// discuss: https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn count_prefix_suffix_pairs(words: Vec<String>) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3042() {
65    }
66}
67


Back
© 2025 bowen.ge All Rights Reserved.