-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAY 21 first Unique character in string.cpp
61 lines (56 loc) · 1.58 KB
/
DAY 21 first Unique character in string.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
//first unique character in a string.cpp
class Solution {
public:
int firstUniqChar(string s) {
map<char,int >m;// m is that initilize variable to char
for(char ans : s) //string read
{
m[ans]++;
}
for (int i=0; i<s.size(); i++)
{
if(m[s.at(i)]==1)
{
return i;
}}
return -1;
}};
// problem 2-
//Ransom Note.cpp leetcode
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char,int> map;
// For loop that holds the frequency of characters in magazine
for(int i = 0; i < magazine.length(); i++)
{
map[magazine[i]]++;
}
// Iterate through ransomNote and check if there are sufficient characters to create ransom note
for(int i = 0; i < ransomNote.length(); i++)
{
if(map[ransomNote[i]] == 0) // No characters available, thus return false
return false;
else
map[ransomNote[i]]--; // Reduce the available chars for char at ransomNote[i]
}
return true;
}
};
// problem 3 leet code
// Valid Anagram .cpp
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.size() != t.size()) return false;
vector<int> counts(26, 0);
for (int i = 0; i < s.size(); ++i) {
counts[s[i] - 'a']++;
counts[t[i] - 'a']--;
}
for (const auto count : counts) {
if (count != 0) return false;
}
return true;
}
};