Count Prefix and Suffix Pairs I

You are given a 0-indexed string array `words`. Define `isPrefixAndSuffix(str1, str2)` to be true when `str1` is both a prefix and a suffix of `str2`. Return the number of index pairs `(i, j)` with `i < j` such that `isPrefixAndSuffix(words[i], words[j])` is true.

30:00

Count Prefix and Suffix Pairs I
easy
Free written solution
Topics
Companies

You are given a 0-indexed string array words. Define isPrefixAndSuffix(str1, str2) to be true when str1 is both a prefix and a suffix of str2. Return the number of index pairs (i, j) with i < j such that isPrefixAndSuffix(words[i], words[j]) is true.

Example 1:
Input: {"words":["a","aba","ababa","aa"]}
Output: 4
Constraints:
  • 1≤words.length≤501 \leq words.\text{length} \leq 50

  • 1≤words[i].length≤101 \leq words[i].\text{length} \leq 10

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

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"words":["a","aba","ababa","aa"]}

words: j (later word) · i (earlier word)

0a
1aba
2ababa
3aa

count = 0. Every later word words[j] will be checked against each earlier word words[i].

pairs counted

0

Variables
VariableValue
count0
DepthFunction Call
Stack empty
0/11