Quick Overview

Given a string, return the index of the first character that occurs exactly once in it, or -1 when every character repeats. It tests character frequency counting, case-sensitive comparison and edge cases such as the empty string.

Index of the First Character That Appears Exactly Once in a String

Company: Autodesk

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

Given a string `s`, return the index of the first character of `s` that occurs exactly once in the whole string. Reading from left to right, the answer is the smallest index `i` such that the character `s[i]` appears at no other position of `s`. If no character of `s` occurs exactly once, including when `s` is empty, return `-1`. ### Function Signature ```python def first_unique_index(s: str) -> int: ``` ### Rules - Characters are compared exactly: an uppercase letter and its lowercase form are different characters, and a space is a character like any other. - Indices are 0-based. ### Constraints - `0 <= len(s) <= 10^5` - Every character of `s` is printable ASCII (codes 32 to 126). - The answer is `-1` or an index from `0` to `len(s) - 1`. ### Examples #### Example 1 ```text Input: s = "swiss" Output: 1 ``` `'s'` occurs three times. `'w'` (index 1) and `'i'` (index 2) each occur once, and index 1 comes first. #### Example 2 ```text Input: s = "Abba" Output: 0 ``` `'A'` and `'a'` are different characters, so each occurs once, and `'b'` occurs twice. The first character that occurs once is `'A'` at index 0. #### Example 3 ```text Input: s = "aabb" Output: -1 ``` Every character occurs twice.

Overview: Given a string, return the index of the first character that occurs exactly once in it, or -1 when every character repeats. It tests character frequency counting, case-sensitive comparison and edge cases such as the empty string.

Read the full Autodesk Software Engineer interview experience this question came from

Given a string `s`, return the index of the first character of `s` that occurs exactly once in the whole string. Reading from left to right, the answer is the smallest index `i` such that the character `s[i]` appears at no other position of `s`. If no character of `s` occurs exactly once, including when `s` is empty, return `-1`. Characters are compared exactly: an uppercase letter and its lowercase form are different characters, and a space is a character like any other. Indices are 0-based. The return value is `-1` or an index from `0` to `len(s) - 1`, so it never exceeds `2^31 - 1` and a 32-bit `int` holds it in every language. ### Example 1 ```text Input: s = "swiss" Output: 1 ``` `'s'` occurs three times. `'w'` (index 1) and `'i'` (index 2) each occur once, and index 1 comes first. ### Example 2 ```text Input: s = "Abba" Output: 0 ``` `'A'` and `'a'` are different characters, so each occurs once, and `'b'` occurs twice. The first character that occurs once is `'A'` at index 0. ### Constraints - `0 <= len(s) <= 10^5` - Every character of `s` is printable ASCII (codes 32 to 126), space included. - The answer is `-1` or an index from `0` to `len(s) - 1`.

Constraints

  • `0 <= len(s) <= 10^5`
  • Every character of `s` is printable ASCII (codes 32 to 126), space included.
  • The answer is `-1` or an index from `0` to `len(s) - 1`.

Examples

Input: ('',)

Expected Output: -1

Explanation: Empty string: no character occurs once, so the answer is -1.

Input: ('z',)

Expected Output: 0

Explanation: A single character occurs once, at index 0.

Hints

  1. Whether `s[i]` qualifies depends on every position of the string, both before and after `i`.
  2. When several characters occur once, the smallest index wins; when none does, including for an empty string, the answer is `-1`.
  3. 'A' and 'a' are different characters, and a space counts like any other character.

Index of the First Character That Occurs Once

easyAutodeskSoftware Engineer

Given a string s, return the index of the first character of s that occurs exactly once in the whole string. Reading from left to right, the answer is the smallest index i such that the character s[i] appears at no other position of s. If no character of s occurs exactly once, including when s is empty, return -1.

Characters are compared exactly: an uppercase letter and its lowercase form are different characters, and a space is a character like any other. Indices are 0-based. The return value is -1 or an index from 0 to len(s) - 1, so it never exceeds 2^31 - 1 and a 32-bit int holds it in every language.

Example 1

Input:  s = "swiss"
Output: 1

's' occurs three times. 'w' (index 1) and 'i' (index 2) each occur once, and index 1 comes first.

Example 2

Input:  s = "Abba"
Output: 0

'A' and 'a' are different characters, so each occurs once, and 'b' occurs twice. The first character that occurs once is 'A' at index 0.

Constraints

  • 0 <= len(s) <= 10^5
  • Every character of s is printable ASCII (codes 32 to 126), space included.
  • The answer is -1 or an index from 0 to len(s) - 1.

Examples

Example 1
Input
('',)
Output
-1
Notes
Empty string: no character occurs once, so the answer is -1.
Example 2
Input
('z',)
Output
0
Notes
A single character occurs once, at index 0.

Constraints

  • •`0 <= len(s) <= 10^5`
  • •Every character of `s` is printable ASCII (codes 32 to 126), space included.
  • •The answer is `-1` or an index from `0` to `len(s) - 1`.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def first_unique_index(s) -> int:
Sample call
first_unique_index('')
Language
Case 1
Input
('',)
Expected
-1
Case 2
Input
('z',)
Expected
0

Browse More Questions

Show the approach

Approach

Count every character in one pass, then scan s again from left to right and return the first index whose character has a count of exactly 1. After the first pass, the count stored for a character equals the number of positions of s that hold it, so s[i] appears at no other position exactly when its count is 1. The second pass visits indices in increasing order, so the first index it returns is the smallest qualifying index, which is the required answer. If the pass ends without a match, no character occurs exactly once and the function returns -1; the empty string runs neither loop and also returns -1. A character that occurs three or more times has a count above 1 and is skipped like one that occurs twice. Comparison is exact, so 'A', 'a' and the space each keep their own count. Shortcuts that look only at neighbors or only at earlier positions fail on inputs such as "abcab", where the repeated 'a' sits at index 0 and the answer is 2. The count table holds at most 95 distinct printable characters, so the extra space is constant.

Time complexity:
O(n)
Space complexity:
O(1)