FastPrepCount Magical Adjacent Substring Triplets
Problems/Count Magical Adjacent Substring Triplets

Count Magical Adjacent Substring Triplets

Google logoGoogle● HardFULLTIMENEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the task and the interview setting. We rebuilt the function signature, the tie-breaking rule, the examples, and the input limits to match the report as closely as we could. Our version is about 90% the same as the reported question.

Two nonempty strings form a magical pair when their sets of distinct lowercase letters are equal.

Return the number of triplets (i,j,k) with i <= j < k for which adjacent substrings text[i..j] and text[j+1..k] form a magical pair.

Function

countMagicalTriplets(text: String) → long

Examples

Example 1

text = "aa"return = 1

The two one-letter substrings have the same set.

Example 2

text = "ababa"return = 4

Several adjacent pairs share {a,b}, with additional one-letter matches.

Example 3

text = "abc"return = 0

No adjacent pair has equal distinct-character sets.

Constraints

  • 0 <= text.length <= 5000.
  • The string contains lowercase English letters.
  • The answer fits in a signed 64-bit integer.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Password Clues and the Next CommandOpenAI · OAOpenAI interview questions
  • Spelling Bee Word FilterBloomberg LP · PHONE SCREENBloomberg LP interview questions
  • Longest Substring with Even OccurrencesDeloitte · OADeloitte interview questions
  • Palindrome-Rearrangeable PairsExpedia · OAExpedia interview questions
  • Palindrome PermutationRippling · OARippling interview questions
  • Repeated DNA SequencesNVIDIA · PHONE SCREENNVIDIA interview questions
  • Numeric Wildcard Dictionary MatchingHeadlands Technologies · PHONE SCREENHeadlands Technologies interview questions
  • Highlight and Rank Overlapping CitationsHarvey · PHONE SCREEN · ONSITE INTERVIEWHarvey interview questions
  • Process Shopping Records, Promotions, and Display OrderInstacart · PHONE SCREEN · ONSITE INTERVIEWInstacart interview questions
  • Minimum Window SubstringAmazon · PHONE SCREEN · ONSITE INTERVIEW

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long countMagicalTriplets(String text) {
        // write your code here
    }
}
Checking account…