Link to home
Start Free TrialLog in
Avatar of angelblade27
angelblade27

asked on

"given a string find the first non-repeated character and return it as a string"

I was asked this in the interview and was trying to find multiple solutions for this problem with its complexity
which is
"given a string find the first non-repeated character and return it as a string"
Avatar of Jai S
Jai S
Flag of India image

you can do this thru regular expressions...i will leave the finding to you...ask me if you cannot figure it out...
wait...i will get back my statemetn....i will try it out first and will comment again...sorry
tht was not so tough...check this tutorial and try out the examples...you will get max knowledge on reg ex patters...
dont think that its in oracle...it also works for c#
http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10795/adfns_re.htm
tht was not so tough...check this tutorial and try out the examples...you will get max knowledge on reg ex patters...
dont think that its in oracle...it also works for c#
http://download.oracle.com/docs/cd/B14117_01/appdev.101/b10795/adfns_re.htm
and this is how you use reg ex in vs.net

--how to use it..
            string str = "MyOhMy@tarzan.com";
            ExtractEmail(str);

the reg ex to extract emails...
private void ExtractEmail(string strSource)
        {
            MatchCollection mc;
            mc = Regex.Matches(strSource,@"([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})");

            for(int i=0;i<mc.Count;i++)
            {
                MessageBox.Show(mc[i].Value);
            }
           
        }
Avatar of Guy Hengel [angelIII / a3]
as the question is an interview question, one simply answer is indeed to use regular expressions (although I am not a guru at that at all :)

another way is to simply loop (pseudo-code)

i = 0
loop
  if string(i) == string(i+1)
    increase i by 2 and loop
  else
    if string(i) == string(i-1)
      increase i by 1 and loop
    else
      string(i) is the first non-repeated character
    end if
  end if  
end loop

int[] visited = new int[MAX_VALUE];

for(int i = 0; i<str.Length; ++i)
     ++visited[str[i]];

for(int i = 0; i<MAX_VALUE; ++i)
    if(visited[i] == 1)
          return;
Avatar of angelblade27
angelblade27

ASKER

perhaps the question wasn't clear and i can understand the assumption
i don't mean if
"aaabbbcddd "
to return 'c' which would be first non-repeating character
but
"teetre"
to return 'r'
One solution is to use hash tables but i was looking for another sudeo code
Supposing it was the first solution angelIII@ i see one problem in ur code
supose there was no non repeating character it would give an exception or return the last character isn't it?
In my code it will return nothing :o)

you can add
throw new Exception("The string does not contain non-repeated character.");
after the second for loop.

int[] visited = new int[MAX_VALUE];

for(int i = 0; i<str.Length; ++i)
     ++visited[str[i]];

for(int i = 0; i<MAX_VALUE; ++i)
    if(visited[i] == 1)
          return (char)i;

throw new Exception("The string does not contain non-repeated character.");
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial