wu :: forums
« wu :: forums - puzzle: find first non repeated character »

Welcome, Guest. Please Login or Register.
Nov 28th, 2024, 10:35am

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: william wu, Eigenray, towr, Icarus, Grimbal, ThudnBlunder, SMQ)
   puzzle: find first non repeated character
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: puzzle: find first non repeated character  (Read 2904 times)
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
puzzle: find first non repeated character  
« on: Dec 9th, 2004, 5:48pm »
Quote Quote Modify Modify

Given an array of characters in array find first non-repeated  character. ex.  in  "total" is 'o'
IP Logged

While we are postponing, life speeds by
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: puzzle: find first non repeated character  
« Reply #1 on: Dec 10th, 2004, 5:57am »
Quote Quote Modify Modify

Create an array of flags, one per character, set them to false.
Scan the string from the end.  For each character: if the flag is false save the character and set the flag to true.
When done, the last saved character is the first non-repeated.
Advantage: for large strings, it is still O(n).
Problem: for short strings and large character sets, you still have to initialize the whole array of flags.
IP Logged
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: puzzle: find first non repeated character  
« Reply #2 on: Dec 10th, 2004, 9:34am »
Quote Quote Modify Modify

where are you going to save the characters? create another array?
IP Logged

While we are postponing, life speeds by
Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: puzzle: find first non repeated character  
« Reply #3 on: Dec 14th, 2004, 3:35am »
Quote Quote Modify Modify

yes.  If the character set is fixed, it is O(1).
IP Logged
Margit
Guest

Email

Re: puzzle: find first non repeated character  
« Reply #4 on: Dec 16th, 2004, 12:47am »
Quote Quote Modify Modify Remove Remove

How does that work with eg. "AAB" ?
IP Logged
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: puzzle: find first non repeated character  
« Reply #5 on: Dec 16th, 2004, 2:14pm »
Quote Quote Modify Modify

Hi I just wrote the alg based on your  psuedo code, but it doesnt work:  
 
 
#include<iostream>
using namespace std;
 
int main()
{
    bool charFlag[26];
    char chars[26], *in="total";
    int last=-1;
     
    for(int i=0;i<26;charFlag[i]=false,i++); // clear the flag arr
     
   for(int i=0;i<=sizeof(in);i++)
   {
  if(charFlag[in[i]-'a']==true)  
      continue;      
  charFlag[in[i]-'a']=true;
  chars[++last]=in[i];
   }
   
    cout<<chars[last]<<endl;  
 system("PAUSE");  
 return 0;
}
IP Logged

While we are postponing, life speeds by
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: puzzle: find first non repeated character  
« Reply #6 on: Dec 16th, 2004, 2:17pm »
Quote Quote Modify Modify

Hi I just wrote the alg based on your  psuedo code, but it doesnt work: (playing with code funcs Smiley)  
 
Code:
 
#include<iostream>  
using namespace std;  
 
int main()  
{  
    bool charFlag[26];  
    char chars[26], *in="total";  
    int last=-1;  
     
    for(int i=0;i<26;charFlag[i]=false,i++); // clear the flag arr  
     
   for(int i=0;i<=sizeof(in);i++)  
   {  
  if(charFlag[in[i]-'a']==true)  
 continue;  
  charFlag[in[i]-'a']=true;  
  chars[++last]=in[i];  
   }  
     
    cout<<chars[last]<<endl;    
 system("PAUSE");  
 return 0;  
}
IP Logged

While we are postponing, life speeds by
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: puzzle: find first non repeated character  
« Reply #7 on: Dec 16th, 2004, 2:35pm »
Quote Quote Modify Modify

I think you missed the 'from the end' part.
Start at the end of the string, and scan to the front.
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: puzzle: find first non repeated character  
« Reply #8 on: Dec 16th, 2004, 3:24pm »
Quote Quote Modify Modify


Sorry, forgot to traverse in reverse... the problem is actually valid of aab... it iwll report a is a last  stored char.....
 
 
 
Code:
i
#nclude<iostream>  
using namespace std;  
   
int main()  
{  
    bool charFlag[26];  
    char chars[26], *in="aab";  
    int last=-1;  
 
    for(int i=0;i<26;charFlag[i]=false,i++); // clear the flag arr  
 
   for(int i=sizeof(in)+1;i>=0;i--)  
   {  
   if(charFlag[in[i]-'a'])    
      continue;  
   charFlag[in[i]-'a']=true;  
   
   last =i;
   
   }    
     
    cout<<in[last]<<endl;    
 system("PAUSE");    
 return 0;  
}  
 
IP Logged

While we are postponing, life speeds by
puzzlecracker
Senior Riddler
****



Men have become the tools of their tools

   


Gender: male
Posts: 319
Re: puzzle: find first non repeated character  
« Reply #9 on: Dec 16th, 2004, 5:45pm »
Quote Quote Modify Modify

oK sorry for the bombardment: here is the correct solution to the problem
 
 
 create a hashtable for each char,  
  zero out every element
  for every occurance of the character, increment the coutner
 go through string array again and if that char hashes to 1, return it.
 
i implemented hash using array
Code:

 
#include<iostream>  
using namespace std;  
   
int main()  
{  
    int charFlag[26];  
    char *in="aab";  
   
 
    for(int i=0;i<26;charFlag[i]=0,i++); // clear the flag arr  
 
   for(int i=0;i<=sizeof(in);++i)  
   charFlag[in[i]-'a']++;    
   for(int i=0;i<=sizeof(in);++i)    
   if(charFlag[in[i]-'a']==1){
  cout<<in[i]<<endl;      
  break;
    }      
   
 system("PAUSE");    
 return 0;  
}  
 

IP Logged

While we are postponing, life speeds by
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board