Условие
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.
Ограничения
0 ≤ length ≤ 100,000
Примеры
Ввод:
text = "swiss"
Ожидаемый вывод:
expected = 1
Ввод:
std::string text = "swiss";
Ожидаемый вывод:
long long expected = 1;
Ввод:
string text = "swiss";
Ожидаемый вывод:
long expected = 1;
Пояснение
s repeats, w appears once at index 1.
Ввод:
text = "aabb"
Ожидаемый вывод:
expected = -1
Ввод:
std::string text = "aabb";
Ожидаемый вывод:
long long expected = -1;
Ввод:
string text = "aabb";
Ожидаемый вывод:
long expected = -1;
Пояснение
Every character repeats: -1.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
You need characters that appear exactly once, not just the first different one.
Подсказка 2
Count all character frequencies first, then find the earliest position with frequency one.
Подсказка 3
Remember that uppercase and lowercase letters are different characters.
Ожидаемая сложность
O(n) time, O(k) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.