簡単 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 つずつ開いてみましょう。

ヒント 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用に調整されています。上記の説明と制約に従ってください。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。