Легко Решено: 0 Процент решений: 0%

First Unique Character

Find the first character that appears only once.

Чему вы научитесь

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

Условие

Показать перевод

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

Примеры

Пример 1
Ввод:
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.

Пример 2
Ввод:
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. Следуйте условию и ограничениям выше.

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.