Count Substrings With Identical Characters
Reported by candidates from Virtu Financial's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Virtu Financial reportedly served this one in July 2025, and the data structure it hinges on is barely a data structure at all: a single running counter over the string. You're asked to count substrings where every character matches, by position. It looks like a string problem, but it's really run-length counting. If you've got an OA coming, this is the kind of question you want to see, because the answer is short and the trap is small. StealthCoder sits behind you as a safety net on the live OA if your mind goes blank, but you shouldn't need it once you see the trick.
The problem
Given a string s, return the number of non-empty substrings in which all characters are identical. Substrings are counted by their positions in s, so equal substring text occurring at different positions is counted separately. Function countIdenticalSubstrings(s: String) → int Examples Example 1 s = "zzzyz" return = 8 There are four one-character substrings equal to "z", two substrings equal to "zz", one substring equal to "zzz", and one substring equal to "y". The total is 4 + 2 + 1 + 1 = 8. Example 2 s = "k" return = 1 The only non-empty substring is "k", whose characters are identical. Constraints 1 <= s.length <= 100 s contains only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Walk the string once and keep a counter for the length of the current run of equal characters. If s[i] equals s[i-1], increment the counter, otherwise reset it to 1. Add the counter to your total at every index. That works because a run of length k contributes 1+2+...+k substrings, and each position adds exactly the number of identical substrings ending there. Check against "zzzyz": counters go 1,2,3,1,1, which sums to 8. The common pitfall is enumerating every substring and checking each one, which is O(n^3) or O(n^2) and unnecessary, though the 100-character limit would let it pass. Another slip is forgetting to reset on a change, or using the k*(k+1)/2 formula and missing the last run. Use a 64-bit total if you're nervous, though 100 characters won't overflow. If you blank live, StealthCoder can hand you the loop in seconds.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count Substrings With Identical Characters cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count number of homogenous substrings. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Virtu Financial's OA.
Virtu Financial reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count Substrings With Identical Characters FAQ
What's the trick to Count Substrings With Identical Characters?+
Track the length of the current run of equal characters. At each index, if it matches the previous one, increment the run length, otherwise reset to 1. Add the run length to the total every step. It's one pass and O(1) extra space.
How hard is this Virtu Financial OA question really?+
Easy. The reported July 2025 version caps the string at 100 characters, so even a brute force would likely pass. The real test is whether you spot the linear approach quickly and handle the single-character case, which returns 1.
Do substrings at different positions count separately?+
Yes. The problem says substrings are counted by position, not by text. In "zzzyz" the single letter z appears four times as a substring and each one counts. That's why you add the run length at every index instead of deduplicating.
Should I use the k*(k+1)/2 formula or the running counter?+
Either works. The running counter is simpler and avoids the off-by-one at the end of the string, where you'd have to flush the last run. The formula needs you to close out each run, including the final one, after the loop.
How do I prepare for this in 48 hours?+
Write the running-counter solution from memory twice, then test it on "zzzyz" (8) and "k" (1). Add a case with all identical characters and one with all different characters. After that, review other run-length and simple counting string problems so the pattern feels familiar.