K-th Non-Repeating Character Index
Problem statement
Given a possibly empty string s and a positive integer k, scan characters from left to right and consider only characters whose total frequency in s is exactly one.
Return the zero-based index of the k-th such character. Return -1 when fewer than k non-repeating characters exist.
Character comparisons are case-sensitive. Every printable ASCII character, including spaces, digits, and symbols, participates normally.
Function
kthNonRepeatingIndex(s: String, k: int) → intExamples
Example 1
s = "swiss"k = 2return = 2The non-repeating characters in encounter order are w at index 1 and i at index 2.
Example 2
s = "a!a?b"k = 2return = 3The unique characters are !, ?, and b. The second appears at index 3.
Example 3
s = ""k = 1return = -1The empty string contains no character, so it has no first unique character.
Constraints
0 <= s.length <= 100000.1 <= k <= 100000.scontains printable ASCII characters.