Count Suffix Word Pairs
Updated
Problem statement
Given an array of strings words, return the number of pairs of indices (i, j) with 0 <= i < j < words.length such that words[i] is a suffix of words[j], or words[j] is a suffix of words[i].
A string s is a suffix of a string t when t ends with s. Every string is a suffix of itself, so two equal strings always form a pair.
Function
countSuffixPairs(words: String[]) → longExamples
Example 1
words = ["back","backdoor","gammon","backgammon","comeback","come","door"]return = 3The pairs are:
words[0]="back"andwords[4]="comeback"words[1]="backdoor"andwords[6]="door"words[2]="gammon"andwords[3]="backgammon"
Example 2
words = ["cba","a","a","b","ba","ca"]return = 8The pairs are:
words[0]="cba"andwords[1]="a"words[0]="cba"andwords[2]="a"words[0]="cba"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"
Example 3
words = ["aa","a","aa"]return = 3"a" is a suffix of both copies of "aa", which gives 2 pairs. The two copies of "aa" are equal, which gives 1 more pair. The answer is 3.
Constraints
1 <= words.length <= 10^51 <= words[i].length <= 10words[i]consists of lowercase English letters.
More TikTok Online Assessment questions
- Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
- Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
- Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
- Shell Command Counts with History ReferencesOA · Seen Oct 2026
- Product Minus Sum of DigitsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Count Case-Insensitive TripletsOA · Seen Aug 2026
- Optimize TikTok Reels ViewingOA · Seen Aug 2026
Similar questions at other companies
- Count One-Swap Number PairsCapital One · OACapital One interview questions
- Maximum Equal Parts for PrefixesAmazon · OA
- Cheating CodemakerJane Street · OAJane Street interview questions
- Count Pairs Differing in One DigitRoblox · OARoblox interview questions
- Candidate Alias Name MatchingCheckr · OA · PHONE SCREEN
- Aggregate Paginated Project Run RecordsReplit · OA
- Maximum Common Decimal Prefix Across ArraysZipRecruiter · OAZipRecruiter interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
TikTok interview experiences
- TikTok AI Infra Intern LLM Round 2: 1 Experience
- TikTok AI Infra Intern DeepSpeed Round: 1 Experience
- TikTok AI Infra Intern Kernel Round: 1 Experience
- TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
- TikTok AI Infra Intern Interview Timeline: 1 Experience
- TikTok AI Infra Intern Onsite Round 2: 1 Experience