Given a string s consisting of small and capital English letters, find and print the first instance of a non-repeating character in it. If there is no such character, print '_' (underscore).
Note that 'A' and 'a' are considered as two different characters.
Input Format
A single line containing the input string.
Constraints
3 < L < 10000 => length(string)
Output Format
A single line containing the resulting character.
Sample Input 0
abacabad
Sample Output 0
c
Explanation 0
There are 2 non-repeating characters in the string: 'c' and 'd'. Print c since it appears in the string first.
Sample Input 1
abacabaabacaba
Sample Output 1
_
Explanation 1
There are no characters in this string that do not repeat.
First Non-Recurring Character - HackerRank Solutions
#include <stdio.h> #include <stdlib.h> #define NO_OF_CHARS 256 int* getCharCountArray(char* str) { int* count = (int*)calloc( sizeof(int), NO_OF_CHARS); int i; for (i = 0; *(str + i); i++) count[*(str + i)]++; return count; } int firstNonRepeating(char* str) { int* count = getCharCountArray(str); int index = -1, i; for (i = 0; *(str + i); i++) { if (count[*(str + i)] == 1) { index = i; break; } } // To avoid memory leak free(count); return index; } int main() { char str[10000]; gets(str); int index = firstNonRepeating(str); if (index == -1) printf("_"); else printf("%c", str[index]); getchar(); return 0; }
No comments:
Post a Comment