समस्या
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।