02 · Arrays & Strings in Java

#387 First Unique Character in a String

Easysolve on LeetCode ↗

frequently reported atGoldman SachsBloombergWalmartAmazonApple

the problem

In plain words

s contains only lowercase letters. Find the first character that appears exactly once in the whole string and return its index. If every character repeats, return -1.

in s = "leetcode"

out 0

'l' appears only once.

in s = "loveleetcode"

out 2

'l' and 'o' repeat; 'v' at index 2 doesn't.

in s = "aabb"

out -1

  • 1 ≤ s.length ≤ 10⁵
  • Lowercase English letters only

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #242
      Valid Anagram
      Easyin Arrays & Strings
    2. #1636
    3. #3