-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path383-Ransom-Note.c
More file actions
executable file
·43 lines (38 loc) · 1.28 KB
/
383-Ransom-Note.c
File metadata and controls
executable file
·43 lines (38 loc) · 1.28 KB
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
#include <stdbool.h>
#include <string.h>
// Returns true if the ransomNote string can be made using individual
// characters from the magazine string, and false if it can't
bool canConstruct(char *ransomNote, char *magazine)
{
// a = 97, z = 122
int n[123] = {0}; // Stores occurances of letters in ransomNote
int m[123] = {0}; // Stores occurances of letters in magazine
int nLength = strlen(ransomNote);
int mLength = strlen(magazine);
// Calculates the number of occurances of each letter in
// ransomNote
for (int i = 0; i < nLength; i++)
{
n[ransomNote[i]]++;
}
// Calculates the number of occurances of each letter in magazine
for (int i = 0; i < mLength; i++)
{
m[magazine[i]]++;
}
// Loops over every letter
for (int i = 97; i < 123; i++)
{
// Checks to see if there are more occurances of a letter in
// ransomNote than in magazine
if (n[i] > m[i])
{
// Returns false if there are as it means ransomNote can't
// be recreated using individual letters from magazine
return false;
}
}
// Returns true if there aren't as it means ransomNote can be
// recreated using individual letters from magazine
return true;
}