Ask a Question
Advertise on boostr.in
Organizational Research By
Surprising Reserch Topic
Find The First Non Repeated Character In A String
First we need to understand the question , we need to write an algorithm for ¬†the first non repeated character in a string , for example :
Read Also : ¬†¬†Count number of words in the String
If the word "stress" is input ¬†then it should print ¬†'t' ¬†¬†as output .
If the word "teeter" is input ¬†then it should print ¬†'r' ¬†¬†as output .
Now , we should understand the pseudo algorithm or logic to achieve this task , code is given in the end of this post .
As we know a non repeated character occurs only once in the string , so if we store the number of times
each alphabet appears in the string , it would help us identifying which characters are non repeated characters in the string .So we need to scan the whole string and determine the final counts of each character . Now scan the final values of each character in the string , the first character in the string with final count 1 is the first non repeated character in the string.
1. ¬†¬†First create the ¬†character count hash table .
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†For each character
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†If there is no value stored in the character
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†set it to 1 .
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†increment the value of the character by 1 .
2. ¬†Scan the string
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†For each character
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†return character if the count in hash table is 1 .
¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†If no character have count 1 , return null
Aug 28, 2015
to add a comment.
Related Hot Questions
Government Jobs Opening