How to Perform a Counting Using Array and Hash Table
Finding the first non-repeating character in a string

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
Search for a command to run...
Finding the first non-repeating character in a string

Hi! My name is Nhut Nguyen. I am a software engineer and a writer in Copenhagen, Denmark.
Learn more about me at nhutnguyen.com
No comments yet. Be the first to comment.
A simple example of using C++ switch

Two dynamic programming techniques to solve Leetcode 120. Triangle. One has space complexity O(n^2). The other is O(n).

An example of using a sliding window approach and an unordered map to track character positions

A simple C++ solution to Leetcode 1695. Maximum Erasure Value using a sliding window approach and prefix sums.

Strategies to avoid them will help you excel in your following technical interview

You have a string called s. Your objective is to locate the index of the first character in the string that does not repeat anywhere else in the string. If such a character doesn't exist, return -1.
Input: s = "leetcode"
Output: 0
Input: s = "loveleetcode"
Output: 2
Input: s = "aabb"
Output: -1
s consists of only lowercase English letters.
#include <iostream>
#include <unordered_map>
using namespace std;
int firstUniqChar(string s)
{
unordered_map<char, int> count;
for (char& c : s)
{
count[c]++;
}
for (int i = 0; i < s.length(); i++)
{
if (count[s[i]] == 1)
{
return i;
}
}
return -1;
}
int main()
{
cout << firstUniqChar("leetcode") << endl;
cout << firstUniqChar("loveleetcode") << endl;
cout << firstUniqChar("aabb") << endl;
}
Output:
0
2
-1
The code initializes an unordered map count where characters from the string are keys, and their corresponding counts (how many times they appear in the string) are values.
The loop then iterates through the characters of the string s. For each character c encountered, it increments its count in the count map.
After counting all the characters in the string, it performs a second loop, this time using an index variable i that starts from 0 and goes up to the length of the string s.
In this loop, it checks whether the count of the character at the current index i is equal to 1. If it is, this means that the character is unique in the string, and the function returns i, which is the index of the first occurrence of that unique character.
If no unique character is found during the loop, the function returns -1 to indicate that there are no unique characters in the string.
This solution has a time complexity of O(n), where n is the length of the string s, because it iterates through the string twice: once to count the characters and once to find the first unique character.
It also has a space complexity of O(k), where k is the number of distinct characters in the string, as it stores character counts in the count unordered map. As the problem considers only lowercase English letters, k = 26, you can give it O(1) complexity.
Runtime: O(n), where n = s.length.
Extra space: O(k), where k is the number of distinct characters in the string. If k=26, it is O(1).
From the constraints "s consists of only lowercase English letters", you can use an array of 26 elements to store the counts.
#include <iostream>
#include <vector>
using namespace std;
int firstUniqChar(string s)
{
// initializes an array of 26 elements, all set to zero
std::array<int, 26> count{};
for (char& c : s)
{
count[c - 'a']++;
}
for (int i = 0; i < s.length(); i++)
{
if (count[s[i] - 'a'] == 1)
{
return i;
}
}
return -1;
}
int main()
{
cout << firstUniqChar("leetcode") << endl;
cout << firstUniqChar("loveleetcode") << endl;
cout << firstUniqChar("aabb") << endl;
}
Output:
0
2
-1
Extra space: O(1) as the array's length is fixed 26.
Thanks for reading! Get my book "10 Classic Coding Challenges" for FREE.