?
Solved

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

Posted on 2007-10-16
12
Medium Priority
?
1,697 Views
Last Modified: 2008-01-09
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"
0
Comment
Question by:angelblade27
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 20091125
you can do this thru regular expressions...i will leave the finding to you...ask me if you cannot figure it out...
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20091129
wait...i will get back my statemetn....i will try it out first and will comment again...sorry
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20091147
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 14

Expert Comment

by:Jai S
ID: 20091150
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
0
 
LVL 14

Expert Comment

by:Jai S
ID: 20091157
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);
            }
           
        }
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 20091161
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

0
 
LVL 9

Expert Comment

by:DrAske
ID: 20092236
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;
0
 

Author Comment

by:angelblade27
ID: 20098788
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
0
 

Author Comment

by:angelblade27
ID: 20098799
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?
0
 
LVL 9

Expert Comment

by:DrAske
ID: 20099180
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.");
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 20099606
if have written this C# code, to extract ALL non-duplicating characters from a string...

            private string GetNonRepeatingCharacter ( string data )
            {
                  //data = "tatrabelle";
                  Boolean blncontinue = true;
                  int offset = 0;

                  while (blncontinue)
                  {
                        if (data.Length <= 1)
                        { blncontinue = false; }
                        else
                        {
                              if (data.Length == data.Replace(data.Substring(offset,1), string.Empty).Length + 1)
                              {
                                    offset ++;
                                    blncontinue = (offset < data.Length);
                              }
                              else
                              {
                                    data =  data.Replace(data.Substring(offset,1), string.Empty);
                                    blncontinue = (offset < data.Length);
                              }
                        }
                  }

                  return data;  
            }

to get the first one, you can use that output and get the first one only, or, in the procedure, replace the 2 lines with (offset++;) by
return data.substring(0,1);

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question