-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCompare_Strings.cpp
45 lines (40 loc) · 1.1 KB
/
Compare_Strings.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
/*
Compare two strings A and B, determine whether A contains all of the characters in B.
The characters in string A and B are all Upper Case letters.
Example
For A = "ABCD", B = "ABC", return true.
For A = "ABCD" B = "AABC", return false.
*/
#include <string>
#include <set>
using namespace std;
class Solution {
public:
/**
* @param A: A string includes Upper Case letters
* @param B: A string includes Upper Case letter
* @return: if string A contains all of the characters in B return true
* else return false
*/
bool compareStrings(string A, string B) {
// write your code here
if (B.empty()) {
return true;
}
if (A.empty()) {
return false;
}
multiset<char> mySet;
for (int i = 0; i < A.length(); i++) {
mySet.insert(A[i]);
}
for (int i = 0; i < B.length(); i++) {
if (mySet.find(B[i]) == mySet.end()) {
return false;
} else {
mySet.erase(mySet.find(B[i]));
}
}
return true;
}
};