Problem · String
Perfect Substrings
Learn this problemProblem statement
Given a digit string s and a positive integer k, count its non-empty substrings in which every digit that appears occurs exactly k times.
Return the total as a 64-bit integer. Two substrings with the same contents are counted separately when they occupy different index ranges.
Function
countPerfectSubstrings(s: String, k: int) → longExamples
Example 1
s = "11020211"k = 2return = 6The six qualifying ranges have contents 11, 110202, 102021, 0202, 020211, and the final 11. Repeated contents at different positions count separately.
Example 2
s = "2222"k = 2return = 3Each length-two range contains the only present digit exactly twice, so all three length-two ranges qualify.
Constraints
1 <= s.length <= 2 * 10^5scontains only digits from0through9.1 <= k <= s.length
More Visa problems
- Connected GroupsOA · Seen Jul 2026
- Maximum Server Processing TimeOA · Seen Jul 2026
- Signal PingsOA · Seen Jul 2026
- Sum of Index-Ordered Pair DifferencesOA · Seen Jul 2026
- Minimum Cost to Attend Required CoursesOA · Seen Jul 2026
- Minimum Score of a Path Between CitiesOA · Seen Jul 2026
- Planning ProductionOA · Seen Jul 2026
- Subarray SumOA · Seen Jul 2026