-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path383. Ransom Note
41 lines (37 loc) · 885 Bytes
/
383. Ransom Note
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
class Solution {
public:
bool canConstruct(string ransomNote, string magazine)
{
int n = ransomNote.length(), m = magazine.length();
sort(ransomNote.begin(), ransomNote.end());
sort(magazine.begin(), magazine.end());
int i=0, j=0, a=0;
if(n>m)
{
return false;
}
else
{
while(i<n && j<m)
{
if(ransomNote[i]==magazine[j])
{
a=1;
i++;
j++;
}
else if(ransomNote[i] > magazine[i])
{
j++;
}
else
{
a=0;
break;
}
}
}
if(a==1 && i==n) return true;
return false;
}
};