25 XP

41. First unique character

Beginner dictionaries strings

Write a function first_unique(text) that takes a string of lower-case letters and returns the index of the first letter that appears exactly once in it. Indexes count from 0. If every letter appears more than once, or the text is empty, return -1.

"First" means first in the text: in "swiss", w and i both appear once, and w comes first.

Example 1

Inputtext = "swiss"
Output1
Explanations is there 3 times; w (index 1) once.

Example 2

Inputtext = "level"
Output2
Explanationl and e appear twice; v once, at index 2.

Example 3

Inputtext = "aabb"
Output-1
ExplanationEvery letter appears twice.

Constraints

  • text has 0 to 100 characters, each a lower-case letter a to z.

Submit also runs 9 more tests you can't see.

Practises Dictionaries (lesson 11).

Code
def first_unique(text):
    """Find the first letter that appears only once.

    text: str, lower-case letters; may be empty
    Returns: int, the index of that letter, or -1 if there
        is none
    """
    # Write your code here
    pass