Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3045 - Count Prefix and Suffix Pairs II\.

    Hard

    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) returns true if str1 is both a prefix and a suffix of str2, and false otherwise.

    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.

    Example 1:

    Input: words = "a","aba","ababa","aa"

    Output: 4

    Explanation: In this example, the counted index pairs are:

    i = 0 and j = 1 because isPrefixAndSuffix("a", "aba") is true.

    i = 0 and j = 2 because isPrefixAndSuffix("a", "ababa") is true.

    i = 0 and j = 3 because isPrefixAndSuffix("a", "aa") is true.

    i = 1 and j = 2 because isPrefixAndSuffix("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 and j = 1 because isPrefixAndSuffix("pa", "papa") is true.

    i = 2 and j = 3 because isPrefixAndSuffix("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:

    • <code>1 <= words.length <= 10<sup>5</sup></code>

    • <code>1 <= wordsi.length <= 10<sup>5</sup></code>

    • words[i] consists only of lowercase English letters.

    • The sum of the lengths of all words[i] does not exceed <code>5 * 10<sup>5</sup></code>.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Long countPrefixSuffixPairs(Array<String> words)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait