Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <limits.h>
- char findFirstNonRepeating(char* str)
- {
- char count[26] = {0}; // O(1)
- char index[26] = {-1}; // O(1)
- // scan the string - O(n)
- for (int i = 0; str[i] != '\0'; ++i)
- {
- char c = str[i];
- ++count[c - 'A'];
- // if this character has not been found yet, mark it as found
- if (index[c - 'A'] == -1)
- index[c - 'A'] = i;
- }
- // find the minimum index at which count is 1. This is O(1).
- int minIndex = INT_MAX;
- for (int i = 0; i < 26; ++i)
- {
- if (index[i] != -1)
- {
- if (count[i] == 1 && index[i] < minIndex)
- minIndex = index[i];
- }
- }
- if (minIndex == INT_MAX)
- return '\0';
- else
- return str[minIndex];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement