Given a string we have to find first repeating character in string and print the result.
Example:
input:aaabasbb
output:a
Logic:
Step 1- make a freq array.
step 2- count character of string
step 3-check in count array and check for first repeating character.
Code-
#include<bits/stdc++.h> using namespace std; int main() { string s; cin>>s; int hash[26]={0}; for(int i=0;i<s.length();i++) { hash[s[i]-'a']++; } for(int i=0;i<s.length();i++) { if(hash[s[i]-'a']>1) { cout<<s[i]<<endl; break; } } }
Output:
aababs a
Time complexity:0(N)
Space Complexity:O(N)