Pocket Gems Interview Question: Write a function that compare... | Glassdoor

Interview Question

Server Engineer Interview San Francisco, CA

Write a function that compares two strings and returns a

  third string containing only the letters that appear in both.
Answer

Interview Answer

1 Answer

1

string getcommstring(string s1,string s2)
{
    string s;
    int size1 = s1.size() ;
    int size2 = s2.size() ;
    int hash[30] ;
    for (int i = 0 ; i < 30 ; i++)
    {
        hash[i] = 0 ;
    }
    for ( int i = 0 ; i < size1 ; i++ )
    {
        hash[s1[i] - 'a'] = 1 ;
    }

    for (int i = 0 ; i < size2 ; i++)
    {
        if ( hash[s2[i] - 'a'] == 1 )
        {
            s.push_back(s2[i]) ;
            hash[s2[i]-'a']= 0 ;
        }
    }

    return s ;
}

Anonymous on Feb 21, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.