Wednesday, December 6, 2017

Intersection of Strings - CTS1

Find the intersection of two given string. Return the string which has character which appears in both string (same sequence order as string1).

Sample Input & Output:
Input: Hello world
Output: lo

Input: placement season
Output: aen

Note: Get input using command line.

Solution:
#include <iostream>
#include <map>
using namespace std;
int main(int c, char *v[])
{
 map<char,int> m;
 int i;
 string x=v[1],y=v[2];
 for(i=0;i<y.length();i++)
    m[y[i]]++;
 for(i=0;i<x.length();i++)
 {
   if(m[x[i]]!=0)
    {
     cout<<x[i];
      m[x[i]]=0;
     }
 }
 return 0;
 }


Share: