Count Prefix and Suffix Pairs I

You are given a 0-indexed string array words.

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.
  • Otherwise, it returns 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
Inputwords = ["a","aba","ababa","aa"]
Output4
The counted pairs are (0, 1), (0, 2), (0, 3), and (1, 2), so the answer is 4.
Example 2
Inputwords = ["pa","papa","ma","mama"]
Output2
The counted pairs are (0, 1) for "pa" and "papa", and (2, 3) for "ma" and "mama", so the answer is 2.

Constraints

  • 1 <= words.length <= 50
  • 1 <= words[i].length <= 10
  • words[i] consists only of lowercase English letters.

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate