3045. Count Prefix and Suffix Pairs II Hard
1/**
2 * [3045] Count Prefix and Suffix Pairs II
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 <= 10^5
40 * 1 <= words[i].length <= 10^5
41 * words[i] consists only of lowercase English letters.
42 * The sum of the lengths of all words[i] does not exceed 5 * 10^5.
43 *
44 */
45pub struct Solution {}
46
47// problem: https://leetcode.com/problems/count-prefix-and-suffix-pairs-ii/
48// discuss: https://leetcode.com/problems/count-prefix-and-suffix-pairs-ii/discuss/?currentPage=1&orderBy=most_votes&query=
49
50// submission codes start here
51
52impl Solution {
53 pub fn count_prefix_suffix_pairs(words: Vec<String>) -> i64 {
54
55 }
56}
57
58// submission codes end
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn test_3045() {
66 }
67}
68
Back
© 2025 bowen.ge All Rights Reserved.