Is permutation

Given two strings, write a method to decide if one is a permutation of the other.

C++:
01 bool isPermutation(string s, string t)
02 {
03     int n=s.length();
04     int m=t.length();
05     if(n!=m)
06         return false;
07     map <char, int> p;
08     for(int i=0;i<n;i++){
09         char c=s[i];
10         if(p.find(c)==p.end())
11             p[c]=1;
12         else
13             p[c]++;
14     }
15     for(int i=0;i<n;i++){
16         char c=t[i];
17         if(p.find(c)==p.end())
18             return false;
19         else{
20             p[c];
21             if(p[c]==0)
22                 p.erase(c);
23         }
24     }
25     return true;
26 }
Advertisements
This entry was posted in String. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s