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.
Einschränkungen
0 ≤ length ≤ 100,000
Beispiele
Eingabe:
text = "swiss"
Erwartete Ausgabe:
expected = 1
Eingabe:
std::string text = "swiss";
Erwartete Ausgabe:
long long expected = 1;
Eingabe:
string text = "swiss";
Erwartete Ausgabe:
long expected = 1;
Erläuterung
s repeats, w appears once at index 1.
Eingabe:
text = "aabb"
Erwartete Ausgabe:
expected = -1
Eingabe:
std::string text = "aabb";
Erwartete Ausgabe:
long long expected = -1;
Eingabe:
string text = "aabb";
Erwartete Ausgabe:
long expected = -1;
Erläuterung
Every character repeats: -1.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
You need characters that appear exactly once, not just the first different one.
Hinweis 2
Count all character frequencies first, then find the earliest position with frequency one.
Hinweis 3
Remember that uppercase and lowercase letters are different characters.
Erwartete Komplexität
O(n) time, O(k) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.