FastPrepFirst Non-Repeating Character

First Non-Repeating Character

Goldman Sachs logoGoldman Sachs● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW

Problem statement

Given a non-empty string text containing lowercase English letters, return its first character that occurs exactly once.

The first character is determined by its position in text, not by alphabetical order. If every character repeats, return the empty string "".

Function

firstNonRepeatingCharacter(text: String) → String

Examples

Example 1

text = "swiss"return = "w"

The letter s repeats. The next character, w, occurs once, so it is the first non-repeating character.

Example 2

text = "aabbc"return = "c"

Both a and b occur twice, while c occurs once.

Example 3

text = "aabb"return = ""

No character occurs exactly once, so the result is the empty string.

Constraints

  • 1 <= text.length <= 10^5
  • text contains only lowercase English letters.

More Goldman Sachs problems

See Goldman Sachs hiring insights
Saved
public String firstNonRepeatingCharacter(String text) {
    // write your code here
}
text"swiss"
expected"w"
Checking account…