Common characters

Write a function f(a, b) which takes two string arguments and returns a
string containing only the characters found in both strings in the order
of O(n)

Answer:


//Use a lookup table, this can be
size_t lookup[26];
// or just a unsigned int, in which the first 26 bits
// is used to mark the presence of a character.
size_t lookup;

Scan the first string and mark the characters in the lookup

Scan the second string, if the character is found in lookup,
then copy it to output string.


No comments: