First non repeating character hash

Problem

Develop an algorithm to print the first none repeating character in a string for example the first none repeating character in the string “baby” is “a”

Hash character in string

This is a typical interview question. A better solution to solve this problem in linear time is to use a hash table on the expense of extra space. Scan the string character by character from left to right while hashing the character. Then scan the hash table looking for the first character that has a count of one.

Code

Here is how to do it in Perl

Thanks for visiting. Please use the comments section below for feedback.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *