Given an array of strings words, find the number of pairs where either the strings are equal or one string ends with another. In other words, find the number of such pairs (i, j) (0 ≤ i < j < words.length) that words[i] is a suffix of words[j].
Examples
01 · Example 1
words = ["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"] return = 3
The relevant pairs are:
words[0] = "hack"andwords[1] = "comeback"words[1] = "hackhour"andwords[6] = "door"words[2] = "jammon"andwords[3] = "backgammon"
02 · Example 2
words = ["cha", "a", "a", "b", "ba", "ca"] return = 8
The relevant pairs are:
words[0] = "cha"andwords[1] = "a"words[0] = "cha"andwords[2] = "a"words[0] = "cha"andwords[4] = "ba"words[1] = "a"andwords[2] = "a"words[1] = "a"andwords[4] = "ba"words[1] = "a"andwords[5] = "ca"words[2] = "a"andwords[4] = "ba"words[2] = "a"andwords[5] = "ca"
Constraints
1 ≤ words.length ≤ 1051 ≤ words[i].length ≤ 10
More Roblox problems
- Candy Crush Grid Matching and GravityPHONE SCREEN · Seen Jun 2026
- Closest Binary Search Tree Value VariantPHONE SCREEN · Seen Jun 2026
- Design Search Autocomplete SystemPHONE SCREEN · Seen Jun 2026
- Find the Closest PalindromePHONE SCREEN · Seen Jun 2026
- Grid Pathfinding with Obstacles (DFS)PHONE SCREEN · Seen Jun 2026
- Maximize Distance to Closest PersonPHONE SCREEN · Seen Jun 2026
- Maximum Number of Balls in a BoxPHONE SCREEN · Seen Jun 2026
- Meeting Rooms IIPHONE SCREEN · Seen Jun 2026
function solution(words: string[]): number {
// write your code here
}
words["hack", "hackhour", "jammon", "backgammon", "comeback", "come", "door"]
expected3
sign in to submit