Tuesday, September 22, 2009

Algorithm

Say you are given two strings A and B where size(A)>size(B). You have to find
in O(n) time if all the characters in B appear in A as well. Repetitions are not
to be considered
example: string A:google string B:log answer:true
string A:google string B:fog answer:false
string A:google string B:lol answer:true

P.S. try and use minimum space...
Octofinder

2 comments:

  1. bool IfSuperSet(B,A)
    1. make BitMap of all the chars possible in B
    2. for i = 0 to length[B]
    3. do if(BitMap[B[i]] == 0)
    4. then BitMap[B[i]] = 1;
    5. for i = 0 to length[A]
    6. do if(BitMap[A[i]] == 1)
    7. then BitMap[A[i]] = 0;
    8. return (BitMap == 0);

    ReplyDelete
  2. I guess hashing will do it........but hash table will be of six=ze 26........so space will be used.......time will be O(n).....

    ReplyDelete