Given a string s, we have to find first non repeating charcter in string
Example:
input:abacd
output:b
Logic:
Step 1- make a freq array.
step 2- count character of string
step 3-check in count array and check for first non 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:
b
Time complexity:0(N)
Space Complexity:O(N)