-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_3.cpp
64 lines (51 loc) · 1.31 KB
/
1_3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// 1_3.cpp
//
//
// Created by Pengyan Qin on 6/14/15.
//
//
#include <iostream>
#include <algorithm> // for sort()
#include <string>
using namespace std;
//time complexity: sort = O(nlogn), total = O(nlogn)
// don't forget to check the case that one of string is emplty
bool is_permutation1(string s1, string s2){
if(s1 == "" || s2 == "")
return false;
if(s1.length() != s2.length())
return false;
sort(s1.begin(), s1.end());
sort(s2.begin(), s2.end());
if(s1 == s2)
return true;
else
return false;
}
// time complexity: total = O(n)
// don't forget to check the case that one of string is emplty
bool is_permutation2(string s1, string s2){
if(s1 == "" || s2 == "")
return false;
if(s1.length() != s2.length())
return false;
int a[128] = {0}; // can use byte operation to reduce the space complexity, refer to 1_1
for(int i = 0; i < s1.length(); ++i){
int n1 = (int)s1[i];
int n2 = (int)s2[i];
a[n1]++;
a[n2]--;
}
for(int i = 0; i < 128; ++i)
if(a[i] != 0)
return false;
return true;
}
int main(){
string s1 = "ab";
string s2 = " ";
cout << is_permutation1(s1, s2) << endl;
cout << is_permutation2(s1, s2) << endl;
return 0;
}