TikTok Twin Pairs
Updated
Problem statement
The TikTok development team is working on a feature to help creators come up with better captions for their videos. TikTok's algorithm favors engaging captions, so you are building an AI tool that paraphrases captions to make them catchier.
However, TikTok charges creators based on the number of characters that get changed after paraphrasing. Therefore, you are tasked with reducing TikTok creators' costs by identifying 'twin-strings' from the captions to avoid paraphrasing the same content multiple times.
Two strings are considered as 'twin-strings' if they are composed of the same characters, regardless of their arrangement or frequency.
For example, "viral" and "rival" are 'twin-strings' since both are composed of the same five characters: 'v', 'i', 'r', 'a', and 'l'. However, "viral" and "trendy" are not 'twin-strings' since they do not share all the same letters.
Given an array of n strings, determine the number of pairs (i, j) such that 0 ≤ i < j < n and (words[i], words[j]) are 'twin-strings.'
Note: Each string is composed of lowercase English characters only.
Function
countTwinPairs(words: String[]) → long
Complete the function countTwinPairs in the editor.
countTwinPairs has the following parameter(s):
string words[n]: an array ofnstrings
Returns
long: the number of valid pairs of 'twin-strings' in the given array.
Examples
Example 1
words = ["abca", "abaa", "baab", "cba"]return = 2Strings "abca" and "cba" are 'twin-strings' because they are made up of the same characters: 'a', 'b' and 'c'. Strings "abaa" and "baab" are 'twin-strings' because they are made up of the same characters: 'a' and 'b'. Thus the pairs (0, 3), (1, 2) are the only valid pairs counted in the answer. Thus the answer is 2.
Example 2
words = ["apple", "apel", "silent", "listen", "papel"]return = 4
Note the following case: Strings "apple", "apel" and "papel" are made up of same unique characters: 'a', 'p', 'e' and 'l'. Thus, any pair of them forms a "twin-string". Strings "silent", "listen" are also "twin-string" because they are made up of the same unique characters: 'l', 'i', 's', 't', 'e' and 'n'. Thus, there are 4 such pairs that satisfy the condition 0 <= i < j < n and (words[i], words[j]) are "twin-string." Therefore, the answer is 4.
Constraints
- 1 ≤
n≤ 10^5 - The sum of the lengths of all strings does not exceed 10^6
- All strings consist of lowercase English characters only.
More TikTok interview questions
- Center of the Largest Perfect X-ShapeOA · Seen Nov 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
- Count Suffix Word PairsOA · Seen Nov 2026
- Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
- Longest Substring Without Repeating CharactersPHONE SCREEN · Seen Oct 2026
- Next Greater Indices and Maximum GapPHONE SCREEN · Seen Oct 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
Similar questions at other companies
- Branch with the Most FilesCapital One · OACapital One interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- First Non-Repeating CharacterAmazon · OA · ONSITE INTERVIEW
- Total Each Letter's Count in an Encoded StringIBM · OAIBM interview questions
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Blood Type Inheritance: Possible Parent GenotypesAbridge · OAAbridge interview questions
- Count Palindrome Addition ChoicesThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Count Palindrome Addition ChoicesHudson River Trading · OAHudson River Trading interview questions
- Count Phrases Formed by Anagram ReplacementWells Fargo · OAWells Fargo interview questions
- Count Repeated Request IDs (Data Science OA)McKinsey & Company · OAMcKinsey & Company 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