All challenges
Easy 0 solved 0% acceptance

First Unique Character

Find the first character that appears only once.

Problem

Return the index of the first character in text that appears exactly once. Case matters.

Implement first_unique_index(text).

Input

text: The text to inspect.

Output

Return the index, or -1 if every character repeats.

Constraints

0 ≤ length ≤ 100,000

Examples

Example 1
Input:
text = "swiss"

Expected output:
expected = 1
Input:
std::string text = "swiss";

Expected output:
long long expected = 1;
Input:
string text = "swiss";

Expected output:
long expected = 1;

Explanation

s repeats, w appears once at index 1.

Example 2
Input:
text = "aabb"

Expected output:
expected = -1
Input:
std::string text = "aabb";

Expected output:
long long expected = -1;
Input:
string text = "aabb";

Expected output:
long expected = -1;

Explanation

Every character repeats: -1.

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.