Given a string s consisting of lowercase English letters, find the first character that repeats in the string. If the string contains no repeating characters, return '#'.
Examples :
Input: s = "geeksforgeeks" Output: 'g' Explanation: 'g', 'e', 'k' and 's' are the repeating characters. Out of these, 'g' occurs first.
Explanation
Input: s = "abcde" Output: '#' Explanation: No repeating character present, so return '#'.
deffirstRep(s:str)->str:foriinrange(len(s)):forjinrange(i+1,len(s)):ifs[i]==s[j]:returns[i]return'#'if__name__=="__main__":s="geeksforgeeks"print(firstRep(s))# Output: g
functionfirstRep(s){for(leti=0;i<s.length;i++){for(letj=i+1;j<s.length;j++){if(s[i]===s[j]){returns[i];}}}return'#';}// Driver Codeconsts="geeksforgeeks";console.log(firstRep(s));// Output: g
Output
g
[Better Approach] Frequency Tracking - O(n) Time and O(1) Space
Since the string contains only lowercase English alphabets, we use a fixed-size frequency array (hash table) of size 26.
In the first pass, we traverse the string and increment the count of each character in our frequency array.
In the second pass, we traverse the string from left to right and check the frequency of each character. The first character we encounter whose frequency is greater than 1 is our answer. This ensures we find the character that repeats first based on its earliest appearance index.
If no character has a frequency greater than 1, we return '#'.
deffirstRep(s:str)->str:hash_arr=[0]*26forcharins:hash_arr[ord(char)-ord('a')]+=1forcharins:ifhash_arr[ord(char)-ord('a')]>1:returncharreturn'#'if__name__=="__main__":s="geeksforgeeks"print(firstRep(s))# Output: g
functionfirstRep(s){lethash=newArray(26).fill(0);for(leti=0;i<s.length;i++){hash[s.charCodeAt(i)-97]++;}for(leti=0;i<s.length;i++){if(hash[s.charCodeAt(i)-97]>1){returns[i];}}return'#';}// Driver Codeconsts="geeksforgeeks";console.log(firstRep(s));// Output: g
Output
g
[Expected Approach] Single Traversal - O(n) Time and O(1) Space
Instead of making two separate passes over the string, we process the string in a single traversal using a Hash Map to track indices.
We iterate through the string from left to right.
If a character is encountered for the first time, we store its index in the map.
If the character is already present in the map, it means it is a repeating character. We compare its first-seen index with our global minimum index tracking variable, updating it if the current index is smaller.
After checking the entire string, the character corresponding to the minimum index found is returned. If no character repeats, we return '#'
importsysdeffirstRep(s:str)->str:mp={}c='#'index=sys.maxsizeforiinrange(len(s)):p=s[i]ifpnotinmp:mp[p]=ielse:ifmp[p]<index:index=mp[p]c=preturncif__name__=="__main__":s="geeksforgeeks"print(firstRep(s))# Output: g
functionfirstRep(s){letmap=newMap();letc='#';letindex=Number.MAX_SAFE_INTEGER;for(leti=0;i<s.length;i++){letp=s[i];if(!map.has(p)){map.set(p,i);}else{if(map.get(p)<index){index=map.get(p);c=p;}}}returnc;}// Driver Codeconsts="geeksforgeeks";console.log(firstRep(s));// Output: g