FastPrepK-th Non-Repeating Character Index

K-th Non-Repeating Character Index

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREEN
Learn

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) → int

Examples

Example 1

s = "swiss"k = 2return = 2

The non-repeating characters in encounter order are w at index 1 and i at index 2.

Example 2

s = "a!a?b"k = 2return = 3

The unique characters are !, ?, and b. The second appears at index 3.

Example 3

s = ""k = 1return = -1

The empty string contains no character, so it has no first unique character.

Constraints

  • 0 <= s.length <= 100000.
  • 1 <= k <= 100000.
  • s contains printable ASCII characters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int kthNonRepeatingIndex(String s, int k) {
    // Write your code here.
}
s"swiss"
k2
expected2
Checking account…