Einfach 0 gelöst 0% Erfolgsquote

First Unique Character

Find the first character that appears only once.

Das lernst du

  • Count characters with a dictionary
  • Make a second pass to find the first unique one
  • Solve it in linear time

Problem

Übersetzung anzeigen

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

Beispiel 1
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.

Beispiel 2
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.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.