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

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।