LeetCode solutions

3309. Count Prefix and Suffix Pairs I

My accepted Python solution to LeetCode problem 3309, Count Prefix and Suffix Pairs I, running in 8ms.

  • Difficulty: Easy
  • Python
  • Runtime 8ms
  • Memory 17.3MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 8ms, memory 17.3MB, accepted 2026-01-02.

python
class Solution:
    def countPrefixSuffixPairs(self, words: List[str]) -> int:
        n = len(words)
        cnt = 0
        for i in range(n):
            for j in range(i+1, n):
                w1, w2 = words[i], words[j]
                if w2.startswith(w1) and w2.endswith(w1):
                    cnt += 1
        return cnt

Source