First Unique Character in a String Python
This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms) problem which is frequently asked in Product based companies like Amazon, Microsoft, Adobe and Google like Maang and other very good product-based Start-up companies which are to find the first unique character in a String and to return its index.
To summarise, Strings in Python Programming language are a series or group of characters which are immutable in nature. We will be using either single or double quotes to declare a string under a variable in any Programming language. We can use the backslash character to escape the usage of quotes power on the String we have declared. We will use the len() function to find the length of the String, and in the same way, we will be using str[n], which is the string name inside the square brackets passing on the integer value of the character that we would like to access.
What are Strings in Python
A String is nothing but a group of characters which can either be words or sentences or even code sometimes. Strings in Python are created using a suitable variable type to the type of data attached to it, and the value of the String that we have written will be declared under single quotes or can be in double quotes.
Creating Strings in Python
Code
Creating_a_String_Variable = “Write your String input value under the double quotes here”
My_String = “javaTpoint.com”
print(My_String)
capital_of_India = “New Delhi”
print(capital_of_India)
Output
javaTpoint.com
New Delhi
Finding the First Unique Character in a String Python
Problem Statement: We are given a String input, and the task is to find the unique character in the String and return the index of the character where it exists. Or, to explain more clearly, we need to find the first non-repeating character in the given String, which is assigned as an input and return the index of the character position in the String. In any test cases that we are provided with, if there is no unique character, then we have to return -1 as an output simply.
Python code
class Solution:
def firstUniqChar(self, s: str) -> int:
#Here, we are trying to leverage the usage of hash maps to store the first element if it occurs
first_unique={}
#Again, here we are using the hashmaps to keep a check on the all characters that we have #visited
string_elem={}
#As a key and value pairs, we will again use a hash mapping to it
#We will here try to iterate over all the characters to check.
for i in range(len(s)):
#below conditioning code will check if the character is visited or not
if s[i] not in string_elem:
#if the condition states that it is not visuited then we will add up to the hashmap
first_unique[s[i]] = i
string_elem[s[i]] = i
#below code will check if the element is unique or if it is repeated character
elif s[i] in first_unique:
#if present then remove it
first_unique.pop(s[i])
#below conditioning code will return the position of the character or the index to say if the #element is unique amongst the characters in the string we are given with
if(len(first_unique)>0):
res = list(first_unique.keys())[0]
return first_unique[res]#otherwise return -1
else:
return -1
Output:
Possible input and output variant- 1
Input: s = “javatpoint”
Output: 0
Possible input and output variant- 2
Input: s = “NiceNice”
Output: -1
Possible C++ code for the same problem
C++ code
class Solution {
public:
int firstUniqChar(string s) {
int freq[26] = {0};
int ans = -1;
for(int i = 0; i<s.size() ; i++){
freq[s[i]-'a']++;
}
for(int i = 0; i<s.size(); i++){
if(freq[s[i]-'a'] == 1){
ans = i;
break;
}
}
return ans;
}
};
Output:
Possible input and output variant- 1
Input: s = “Ababbaacferty”
Output: 7
Possible JavaScript code for the same problem
Code
const firstUniqChar = (s) => {
let hashTable = {};
for (let i = 0; i <= s.length - 1; i++) {
if (hashTable.hasOwnProperty(s[i])) {
hashTable[s[i]] = 2;
} else {
hashTable[s[i]] = 1;
}
}
for (let i = 0; i <= s.length - 1; i++) {
if (hashTable.hasOwnProperty(s[i]) && hashTable[s[i]] === 1) {
return i;
}
}
return -1;
};
Output:
Possible input and output variant- 1
Input: s = “ aabbaa”
Output: -1