Ctrl K

Bookend Word Pairs

Easy
Say that a word a bookends a word b if b both starts and ends with a. For example, "aba" bookends "ababa", while "ab" does not bookend "abc". Every word bookends itself. Given the list words, return the number of index pairs i < j where words[i] bookends words[j].
Example 1
words=["ok", "okok", "o", "oko"] → 2

The pairs are "ok" with "okok" and "o" with "oko". "okok" is not framed by "o" because it ends in k.

Example 2
words=["abcab", "ab"] → 0

The short word comes after the long one, and only pairs where the framing word comes first count.

Example 3
words=["x", "x", "xx"] → 3

All three pairs count: "x"–"x", "x"–"xx", and the second "x"–"xx".

Constraints
  • 1 ≤ len(words) ≤ 50
  • 1 ≤ len(words[i]) ≤ 20
  • Words consist of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFraming Word Pairs HardTarget From Word Beginnings MediumPhrase From Word Beginnings, Long Edition HardPalindrome by Adding Letters in Front HardHighlight Search Hits Medium
loading Python…
Input
words=["ok", "okok", "o", "oko"]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.