Problem
You are given a 0-indexed string array words
.
Let’s define a boolean function isPrefixAndSuffix
that takes two strings, str1
and str2
:
isPrefixAndSuffix(str1, str2)
returnstrue
ifstr1
is both a prefix and a suffix ofstr2
, andfalse
otherwise.
A prefix of a string is a substring that starts from the beginning of the string and extends to any point within it.
A suffix of a string is a substring that begins at any point in the string and extends to its end.
For example, isPrefixAndSuffix("aba", "ababa")
is true
because "aba"
is a prefix of "ababa"
and also a suffix, but isPrefixAndSuffix("abc", "abcd")
is false
.
Return an integer denoting the number of index pairs (i, j)
such that i < j
, and isPrefixAndSuffix(words[i], words[j])
is true
.
https://leetcode.com/problems/count-prefix-and-suffix-pairs-i/
Example 1:
Input:
words = ["a","aba","ababa","aa"]
Output:4
Explanation: In this example, the counted index pairs are:
i = 0
andj = 1
becauseisPrefixAndSuffix("a", "aba")
is true.
i = 0
andj = 2
becauseisPrefixAndSuffix("a", "ababa")
is true.
i = 0
andj = 3
becauseisPrefixAndSuffix("a", "aa")
is true.
i = 1
andj = 2
becauseisPrefixAndSuffix("aba", "ababa")
is true.
Therefore, the answer is 4.
Example 2:
Input:
words = ["pa","papa","ma","mama"]
Output:2
Explanation: In this example, the counted index pairs are:
i = 0
andj = 1
becauseisPrefixAndSuffix("pa", "papa")
is true.
i = 2
andj = 3
becauseisPrefixAndSuffix("ma", "mama")
is true.
Therefore, the answer is 2.
Example 3:
Input: words = ["abab","ab"]
Output: 0
Explanation: In this example, the only valid index pair is i = 0
and j = 1
, and isPrefixAndSuffix("abab", "ab")
is false.
Therefore, the answer is 0.
Constraints:
1 <= words.length <= 50
1 <= words[i].length <= 10
words[i]
consists only of lowercase English letters.
Test Cases
1 | class Solution: |
1 | import pytest |
Thoughts
直接类似 1408. String Matching in an Array 那样,遍历所有的单词对。
时间复杂度 O(n²)
,空间复杂度 O(1)
。
本题单词长度可以认为是常数,否则如果单词长度为 k,则时间复杂度
O(k * n²)
。不过本题的 k 和 n 都很小,优化了时间复杂度可能反倒系数很大导致运行更慢。
Code
1 | from itertools import combinations |