FastPrepIndex of the First Non-Repeating Character

Index of the First Non-Repeating Character

Bloomberg LP logoBloomberg LP● EasyNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

Given a non-empty string s, return the zero-based index of the first character whose total frequency in the string is exactly one.

If no such character exists, return -1. Character comparisons are case-sensitive.

Function

firstNonRepeatingIndex(s: String) → int

Examples

Example 1

s = "swiss"return = 1

The character s repeats, while w occurs once at index 1.

Example 2

s = "aabbc"return = 4

Only c has frequency one, and its index is 4.

Example 3

s = "aabb"return = -1

Every character repeats, so there is no qualifying index.

Constraints

  • 1 ≤ s.length ≤ 100000.
  • s contains printable ASCII characters.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int firstNonRepeatingIndex(String s) {
    // Write your code here.
}
s"swiss"
expected1
Checking account…