Write an efficient algorithm to find the first non-repeated character in a string defined

Write an efficient algorithm to find the first non-repeated character in a string defined
over the English alphabet set [a-z, A-Z].
For example, the first non-repeated character in teeter is r.
Analyze the time complexity of your algorithm.
NOTE:Time complexity should be linear not quadratic .

Replies

  • Ankita Katdare
    Ankita Katdare
    A simple algorithm should be:

    Step 1. Scanning the string from left to right and then storing the values in a count array.
    Step 2. Scanning the string from left to right again and checking for count of each character, if an element who's count is 1 is found, return it.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    AbraKaDabra
    A simple algorithm should be:

    Step 1. Scanning the string from left to right and then storing the values in a count array.
    Step 2. Scanning the string from left to right again and checking for count of each character, if an element who's count is 1 is found, return it.
    AKD, the algorithm of yours seems to be quadratic time complexity!
  • Ankita Katdare
    Ankita Katdare
    Praveen-Kumar
    AKD, the algorithm of yours seems to be quadratic time complexity!
    Hmm, yes. O(n2) #-Link-Snipped-# write one algo here for us all.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    AbraKaDabra
    Hmm, yes. O(n2) #-Link-Snipped-# write one algo here for us all.
    Er... Will try... If PHP I guess I can easily say... There is a concept of associative arrays, where you can do this way: count["char"] = n and so on.

    For the algorithm,
    1. Define an empty associative array and initialize to 0. def count[] = 0;
    2. For each character, check the occurrence and increment the particular array node. count["s"]++;
    3. Loop through the array and get the first value, which is 1!
    foreach (val as count)
      if val is 1
        print the index
    The complexity is O(n).
  • silverscorpion
    silverscorpion
    A simple algorithm to do it in only one pass of the string is to maintain an array of count of the letters, and then scanning through the count array instead of the string itself again.

    The algorithm is like:

    initialize count[256][2] to all zeros
    read S
    for i in range 0 to length(S):
        if count[S[i]][0] == 0:
            count[S[i]][1] = i
        count[S[i]][0]++
    Index = length(S)
    for i in range 1 to 255:
        if count[i][0] == 1:
            if count[i][1] < Index:
                Index = count[i][1]
    return S(Index)
    I hope the pseudo code is clear..

    The running time is O(n+c) where c is 256 here. So, essentially it is O(n)
  • silverscorpion
    silverscorpion
    Praveen-Kumar
    Er... Will try... If PHP I guess I can easily say... There is a concept of associative arrays, where you can do this way: count["char"] = n and so on.

    For the algorithm,
    1. Define an empty associative array and initialize to 0. def count[] = 0;
    2. For each character, check the occurrence and increment the particular array node. count["s"]++;
    3. Loop through the array and get the first value, which is 1!
    foreach (val as count)
      if val is 1
        print the index
    The complexity is O(n).
    I think that won't work.. If you are using associative arrays, you need to specify the count of the character as well as the index when it was first encountered.

    For example, take the string "bbzaccddbcd"
    If you fill an associative array with this string, with the key as the characters, you would get

    array['b']=3, array['c']=3, array['a']=1, etc.. and array['z']=1

    Now you will scan through the array and the first element that has the value 1 is 'a'. So, you will return a, while the answer is z.

    That's why, you also need to store the index where each character is first encountered. Then compare that value also.. Just checking for the count being 1 is not enough.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    silverscorpion
    I think that won't work.. If you are using associative arrays, you need to specify the count of the character as well as the index when it was first encountered.

    For example, take the string "bbzaccddbcd"
    If you fill an associative array with this string, with the key as the characters, you would get

    array['b']=3, array['c']=3, array['a']=1, etc.. and array['z']=1

    Now you will scan through the array and the first element that has the value 1 is 'a'. So, you will return a, while the answer is z.

    That's why, you also need to store the index where each character is first encountered. Then compare that value also.. Just checking for the count being 1 is not enough.
    Hey wait... Coming to what you said, consider the string "bbzaccddbcd":
    count["b"]++;
    count = {
        "b" = 1
    }
    count["b"]++;
    count = {
        "b" = 2
    count["z"]++;
    }
    count = {
        "b" = 2,
        "z" = 1
    }
    ...
    ...
    ...
    ...
    count["d"]++;
    count = {
        "b" = 2,
        "z" = 1,
        "a" = 1,
        "c" = 3,
        "d" = 3
    }
    When the loop encounters the first value of 1, it returns "z" and not "a". Hope you are clear.

You are reading an archived discussion.

Related Posts

Imagine a cubic array made up of an n*n*n arrangement of unit cubes: the cubic array is n cubes wide, n cubes high and n cubes deep. A special case...
The integers 1, 2,........., 10 are circularly arranged in an arbitrary order. Show that there are always three successive integers in this arrangement, whose sum is at least 17.
Marrying advanced micro electronics to biomimetics, UK and US researchers hope to give birth to cybeplasm microbots with a starting size of 1 cm that may well shrink to a...
The following is the story of Mr. Divyank Turakhia - the 30 year old guy who started his business at the age of 14; freelancing for his clients. At the...
CEans, We've installed a little survey tool to gather anonymous feedback from our users. The system shows a pop-up message and asks you to fill up a small questionnaire (takes...