3. EV_First Unique Character asked in infosys on hackerank platform

PHOTO EMBED

Sun Mar 06 2022 22:35:30 GMT+0000 (Coordinated Universal Time)

Saved by @ayushshuklas #c++

#include <bits/stdc++.h>
using namespace std;  

int uniquestring(string s)
{
      int a[26];
for(int i=0;i<26;i++)
a[i]=0;
for(int i =0;i<s.length();i++)
{
    int m = s[i];
    a[m - 97]++;
}

for(int i =0;i<s.length();i++)
{
    int t = s[i];
    if(a[t-97] == 1)
    {
   return i+1;
    }
}
return -1;
}
int main()  
{  
   map<char, int> wordFreq;
    string s, t;
    cin >> s;
 cout << uniquestring(s);
}  
content_copyCOPY

A unique character is one that appears only once in a string. Given a string consisting of lowercase English letters only, return the index of the first occurrence of a unique character in the string using 1-based indexing. If the string does not contain any unique character, return-1.

https://ayushshuklas.blogspot.com