Member-only story
Interview question: Print first character of a string that is not repeated
When a person goes to a coding interview, he is likely to be asked to solve coding problems regarding strings.
One question that some may find difficult to solve is the question of printing the first character of a string that is not repeated.
The psuedocode for solving this problem is:-
- Define the function, no_repeat, which accepts a string as its input.
- In the function, create a while loop that will loop through the string as long as it is not empty.
- Within the while loop, define the variable slen0, which is the length of the string. Define the variable, ch, which is the 0th element of the string.Replace the variable, ch, with a blank string. Define the variable, slen1, which is the length of the string.
- Within the while loop, if slen1 = slen0–1 then print the non repeating character and then break out of the loop. Otherwise, print that no unique characters were found.
The code in Python for this problem is found below:-
The solution to this problem is a little more difficult and will take a fair amount of study to get the grasp of. The best thing to do is to study the algorithm until it is totally understood.