Đề bài
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.
Ràng buộc
0 ≤ length ≤ 100,000
Ví dụ
Đầu vào:
text = "swiss"
Kết quả mong đợi:
expected = 1
Đầu vào:
std::string text = "swiss";
Kết quả mong đợi:
long long expected = 1;
Đầu vào:
string text = "swiss";
Kết quả mong đợi:
long expected = 1;
Giải thích
s repeats, w appears once at index 1.
Đầu vào:
text = "aabb"
Kết quả mong đợi:
expected = -1
Đầu vào:
std::string text = "aabb";
Kết quả mong đợi:
long long expected = -1;
Đầu vào:
string text = "aabb";
Kết quả mong đợi:
long expected = -1;
Giải thích
Every character repeats: -1.
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
You need characters that appear exactly once, not just the first different one.
Gợi ý 2
Count all character frequencies first, then find the earliest position with frequency one.
Gợi ý 3
Remember that uppercase and lowercase letters are different characters.
Độ phức tạp mong đợi
O(n) time, O(k) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.