Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion Leetcode/0952-word-subsets/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,39 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
vector<string> wordSubsets(vector<string>& words1, vector<string>& words2) {
vector<string> ans;
vector<int> maxFreq(26, 0);
for (const string& word : words2) {
vector<int> freq(26, 0);
for (char c : word) {
freq[c - 'a']++;
}
for (int i = 0; i < 26; i++) {
maxFreq[i] = max(maxFreq[i], freq[i]);
}
}
for (const string& word : words1) {
vector<int> freq(26, 0);
for (char c : word) {
freq[c - 'a']++;
}
bool isSubset = true;
for (int i = 0; i < 26; i++) {
if (freq[i] < maxFreq[i]) {
isSubset = false;
break;
}
}
if (isSubset) {
ans.push_back(word);
}
}
return ans;
}
};
```

</template>
Expand Down
15 changes: 14 additions & 1 deletion Leetcode/1510-find-lucky-integer-in-an-array/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,20 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int findLucky(vector<int>& arr) {
map<int,int> mpp;
for(auto &it : arr) mpp[it]++;
int res = -1;
for(auto it : mpp){
if(it.first == it.second){
res = max(res, it.first);
}
}
return res;
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,29 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
private:
void solve(string &curr, vector<string> &res, int n){
if(curr.size() == n){
res.push_back(curr);
return;
}
for(char ch = 'a'; ch <= 'c'; ch++){
if(!curr.empty() && curr.back() == ch) continue;
curr.push_back(ch);
solve(curr, res, n);
curr.pop_back();
}
}
public:
string getHappyString(int n, int k) {
string curr = "";
vector<string> res;
solve(curr, res, n);
if(res.size() < k) return "";
return res[k - 1];
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,41 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int maxDiff(int num) {
auto replace = [](string& s, char x, char y) {
for (char& digit : s) {
if (digit == x) {
digit = y;
}
}
};
string min_num = to_string(num);
string max_num = to_string(num);
for (char digit : max_num) {
if (digit != '9') {
replace(max_num, digit, '9');
break;
}
}
for (int i = 0; i < min_num.size(); ++i) {
char digit = min_num[i];
if (i == 0) {
if (digit != '1') {
replace(min_num, digit, '1');
break;
}
} else {
if (digit != '0' && digit != min_num[0]) {
replace(min_num, digit, '0');
break;
}
}
}
return stoi(max_num) - stoi(min_num);
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,27 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int n = cardPoints.size();
int lsum = 0;
int rsum = 0;
int maxsum = 0;
for(int i = 0 ; i < k ; i++){
lsum += cardPoints[i];
}
maxsum = lsum;
int right = n - 1;
for(int i = k - 1 ; i >= 0 ; i--){
lsum -= cardPoints[i];
rsum += cardPoints[right];
maxsum = max(maxsum , lsum + rsum);
right--;
}
return maxsum;
}
};
```

</template>
Expand Down
26 changes: 25 additions & 1 deletion Leetcode/1558-course-schedule-iv/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,31 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
vector<bool> checkIfPrerequisite(int numCourses, vector<vector<int>>& prerequisites, vector<vector<int>>& queries) {
map<int, set<int>> reachable;
for(auto it : prerequisites){
reachable[it[1]].insert(it[0]);
}
int n = numCourses;
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < n ; j++){
if(reachable[j].count(i)){
reachable[j].insert(reachable[i].begin() , reachable[i].end());
}
}
}
vector<bool> res;
for(int i = 0 ; i < queries.size() ; i++){
if(reachable[queries[i][1]].count(queries[i][0])){
res.push_back(true);
}
else res.push_back(false);
}
return res;
}
};
```

</template>
Expand Down
18 changes: 17 additions & 1 deletion Leetcode/1561-rearrange-words-in-a-sentence/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,23 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
string arrangeWords(string text) {
text[0] = text[0] - 'A' + 'a';
map<int, vector<string>> mp;
stringstream ss(text);
string word;
while (ss >> word) mp[word.size()].push_back(word);
string ans;
for (auto &x : mp) {
for (string &v : x.second) ans += v + " ";
}
ans.pop_back();
ans[0] = toupper(ans[0]);
return ans;
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,23 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int isPrefixOfWord(string sentence, string searchWord) {
int idx = 1;
int start = 0;
for (int i = 0; i <= sentence.size(); i++){
if (sentence[i] == ' ' ||i == sentence.size()){
string prefix = sentence.substr(start, searchWord.size());
if (searchWord == prefix)
return idx;
start = 1 + i;
idx++;
}
Comment on lines +88 to +95
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix out-of-bounds array access in loop.

Line 88 loops with condition i <= sentence.size(). When i == sentence.size(), line 89 accesses sentence[i], which is out of bounds (undefined behavior). The boundary check must occur before the access.

🔎 Proposed fix for out-of-bounds access
-        for (int i = 0; i <= sentence.size(); i++){
-            if (sentence[i] == ' ' ||i == sentence.size()){
+        for (int i = 0; i < sentence.size(); i++){
+            if (sentence[i] == ' '){
                 string prefix = sentence.substr(start, searchWord.size());
                 if (searchWord == prefix)
                     return idx;
                 start = 1 + i;
                 idx++;
             }
         }
+        // Check the last word after the loop
+        string prefix = sentence.substr(start, searchWord.size());
+        if (searchWord == prefix)
+            return idx;
🤖 Prompt for AI Agents
In
Leetcode/1566-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/index.md
around lines 88 to 95, the loop currently uses i <= sentence.size() but accesses
sentence[i] before checking bounds which causes out-of-bounds when i ==
sentence.size(); fix by checking the boundary first: change the if to "if (i ==
sentence.size() || sentence[i] == ' ')" so you never index sentence at position
sentence.size(); keep the rest of the logic (substr and start/idx updates) the
same.

}
return -1;
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,26 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
vector<int> finalPrices(vector<int>& prices) {
int n = prices.size();
stack<pair<int,int>> st;
vector<int> res(n, 0);
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && st.top().first > prices[i]) {
st.pop();
}
if (!st.empty()) {
res[i] = prices[i] - st.top().first;
} else {
res[i] = prices[i];
}
st.push({prices[i], i});
}
return res;
}
};
```

</template>
Expand Down
16 changes: 15 additions & 1 deletion Leetcode/1585-the-kth-factor-of-n/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,21 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
int kthFactor(int n, int k) {
vector<int> factors;
for(int i = 1; i * i <= n ; i++){
if(n % i == 0){
factors.push_back(i);
if(n / i != i) factors.push_back(n / i);
}
}
sort(factors.begin(), factors.end());
if(k > factors.size()) return -1;
return factors[k - 1];
}
};
```

</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,31 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
public:
const int mod=1000000007;
int numSubseq(vector<int>& nums, int target) {
int n = nums.size();
sort(nums.begin() , nums.end());
vector<int> noOfsub(n + 1 , 1);
for(int i = 1 ; i <= n ; i++){
noOfsub[i] = (2 * noOfsub[i - 1]) % mod;
}
int i = 0;
int j = n - 1;
int res = 0;
while(i <= j){
if(nums[j] + nums[i] <= target){
res = (res + noOfsub[j - i]) % mod;
i++;
}
else{
j--;
}
}
return res;
}
};
```

</template>
Expand Down
33 changes: 32 additions & 1 deletion Leetcode/1628-count-submatrices-with-all-ones/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,38 @@ class Solution {
<template #cpp>

```cpp
// Add your C++ solution here
class Solution {
private:
int findRes(vector<int>& temp){
int res = 0;
int consec = 0;
for(auto it : temp){
if(it == 0){
consec = 0;
} else {
consec++;
}
res += consec;
}
return res;
}
public:
int numSubmat(vector<vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
int res = 0;
for(int srow = 0; srow < n; srow++){
vector<int> v(m, 1);
for(int erow = srow; erow < n; erow++){
for(int col = 0; col < m; col++){
v[col] = v[col] & mat[erow][col];
}
res += findRes(v);
}
}
return res;
}
};
```

</template>
Expand Down