Pls help :Find first non-repeated char in a string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 3
Reputation: varsha_saxena is an unknown quantity at this point 
Solved Threads: 0
varsha_saxena varsha_saxena is offline Offline
Newbie Poster

Pls help :Find first non-repeated char in a string

 
0
  #1
Feb 24th, 2007
Hi Pls help me to write first non-repeated char in string . eg. If string is abcda then first non-repeated char is b . I want an efficient algo / program which will traverse the string only n times . I don't want to use hash which set bit if it encounters char .
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Pls help :Find first non-repeated char in a string

 
0
  #2
Feb 24th, 2007
Do you want us to give you an algorithm or help you fix one you've already got? If give, read this. If fix, we're not psychic. Whatcha got?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: varsha_saxena is an unknown quantity at this point 
Solved Threads: 0
varsha_saxena varsha_saxena is offline Offline
Newbie Poster

Re: Pls help :Find first non-repeated char in a string

 
0
  #3
Feb 24th, 2007
Hi, I am sorry if I have violated any rules of this forum. I was interviewed and they asked me this question . I was able to provide the below Algo .But he wanted me to give a solution without using 2 - dimensional arrays.I was unable to provide a solution
Algo
1. 2 dimensional array which will store all characters we encountered in string one by one and also number of times those char occurred in the string
2. For each char in original string , if no value stored in second 2 dim array then store 1.
3. after scanning the string array , scan the 2 dim array and return if count is 1.

Array 1 : teeth
Array 2 : t 2
e 2
h 1

return h

char FirstNonRepeated(String str)
{
Int length = strlen(str)
Char hashString[length][2];

hashString[0]=str[0];

For(i=1;i<length;i++)
{
idxrow=0;
While(idxrow!=length )
{
If(str[i] == hashString[idxrow][0]
{
hashString[idxrow][1] = hashString[idxrow][1] +1 ;
Exit;
}
Else
{
idxrow++;
If(idxrow ==length) //reached end but char not found
{
hashString[secondoccurence][1]=1; //first
occurrence of element
return str[i];
}
}//end of else



}//end of while
}//end of for

}//end of func
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,815
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Pls help :Find first non-repeated char in a string

 
1
  #4
Feb 24th, 2007
I hope that your code isn't supposed to be valid C, because it's not.

>But he wanted me to give a solution without using 2 - dimensional arrays.
Well, you can do it with a frequency table, which is essentially a one dimensional array:
  1. char FirstNonRepeated ( const char *str )
  2. {
  3. // unsigned char for space efficiency
  4. unsigned char saved[CHAR_MAX + 1] = {0};
  5. size_t i;
  6.  
  7. // Build the frequency table
  8. for ( i = 0; str[i] != '\0'; i++ )
  9. ++saved[str[i]];
  10.  
  11. // Return the first character with no repeats
  12. for ( i = 0; str[i] != '\0'; i++ ) {
  13. if ( saved[str[i]] < 2 )
  14. return str[i];
  15. }
  16.  
  17. return '\0'; // No non-repeated chars
  18. }
In C++ I would expect to see you use a map or hashmap, and if you used an array I would ask you to defend your decision. If you used a map or hashmap I would ask you to defend your decision too, so don't read into that. In an interview we try to get a good feel for how your brain works.

You can also solve the problem in a less elegant manner using nested loops:
  1. char FirstNonRepeated ( const char *str )
  2. {
  3. size_t i;
  4.  
  5. for ( i = 0; str[i] != '\0'; i++ ) {
  6. // Find any repeats
  7. int is_repeated = 0;
  8. size_t j;
  9.  
  10. for ( j = 0; str[j] != '\0'; j++ ) {
  11. // Make sure the character skips itself
  12. if ( i != j && str[j] == str[i] ) {
  13. is_repeated = 1;
  14. break;
  15. }
  16. }
  17.  
  18. if ( !is_repeated )
  19. return str[i];
  20. }
  21.  
  22. return '\0'; // Non-repreated chars
  23. }
Those are the two solutions I would expect to see from an interview candidate, with the former coming from someone applying for a senior position and the latter from someone applying for a junior position with no experience.

Because the question is meant to show your foundation logic, I would probably consider the nested loop function to be a failing solution most of the time even though it works. Likewise with anything more cryptic than necessary. The former suggests a weak programmer and the latter suggests a programmer with a habit of over-engineering things. A weak programmer still might make the cut if the job has some wiggle room for mentoring or if the candidate is open to alternatives.
Last edited by Narue; Feb 24th, 2007 at 11:48 pm. Reason: Fixed a minor bug ;)
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,042
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Pls help :Find first non-repeated char in a string

 
0
  #5
Feb 24th, 2007
@Narue

I'm a little lost with this statement in the first for loop :
  1. ++saved[str[i]];
What exactly is this line doing?.
First increment saved to what?
Second saved[whatever is pointer[index] ?]

Could you explaning it for me, please?.

Also I have a question with this line:

  1. unsigned char saved[CHAR_MAX] = {0};
How big should I define CHAR_MAX?.

Thank you!
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,815
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: Pls help :Find first non-repeated char in a string

 
0
  #6
Feb 24th, 2007
>What exactly is this line doing?
It's using a character in the string as an index into the saved array and incrementing the value stored there. The values start at zero, so you're basically counting how many of each character is stored in the string. Let's say for the sake of brevity that a char can only hold the numbers 0 through 5 and the string contains "1515325". The saved array starts out like this:
  1. [0][0][0][0][0][0]
Going into the loop, we use each number in the string as an index into the saved array. After the loop is done, the saved array looks like this:
  1. [0][2][1][1][0][3]
saved[1] is 2 because the string has two 1's, saved[5] is 3 because there are that many 5's in the string, and saved[2] and saved[3] are both 1 for the same reason. That's a frequency table. The value you want a frequency of is used as an index and the value stored in the table represents how many times it occurs.

The problem with a frequency table like this is that the table has to be as big as all of the possible values. For small ranges like the latin alphabet or even the ASCII character set, that's okay. An array of 256 values isn't a big hit, but if you have millions or billions of values in the range, it becomes more of an issue for storage.

>How big should I define CHAR_MAX?
CHAR_MAX is a standard macro defined in limits.h. It tells you the largest value that fits in a char.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: varsha_saxena is an unknown quantity at this point 
Solved Threads: 0
varsha_saxena varsha_saxena is offline Offline
Newbie Poster

Re: Pls help :Find first non-repeated char in a string

 
0
  #7
Feb 25th, 2007
Thanks a lot . I appreciate your efforts .
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC