-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fa35e4
commit 7876167
Showing
10 changed files
with
474 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
08_String/Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
884. Uncommon Words from Two Sentences | ||
A sentence is a string of single-space separated words where each word consists only of lowercase letters. | ||
A word is uncommon if it appears exactly once in one of the sentences, and does not appear in the other sentence. | ||
Given two sentences s1 and s2, return a list of all the uncommon words. You may return the answer in any order. | ||
Example 1: | ||
Input: s1 = "this apple is sweet", s2 = "this apple is sour" | ||
Output: ["sweet","sour"] | ||
Explanation: | ||
The word "sweet" appears only in s1, while the word "sour" appears only in s2. | ||
Example 2: | ||
Input: s1 = "apple apple", s2 = "banana" | ||
Output: ["banana"] | ||
Constraints: | ||
1 <= s1.length, s2.length <= 200 | ||
s1 and s2 consist of lowercase English letters and spaces. | ||
s1 and s2 do not have leading or trailing spaces. | ||
All the words in s1 and s2 are separated by a single space. | ||
*/ | ||
class Solution { | ||
public: | ||
vector<string> uncommonFromSentences(string s1, string s2) { | ||
vector<string> ans; | ||
map<string,int> map1,map2; | ||
string str = ""; | ||
for(char ch:s1){ | ||
if(ch!=' ') str+=ch; | ||
else{ | ||
map1[str]++; | ||
str=""; | ||
} | ||
} | ||
map1[str]++; | ||
str=""; | ||
|
||
for(char ch:s2){ | ||
if(ch!=' ') str+=ch; | ||
else{ | ||
map2[str]++; | ||
str=""; | ||
} | ||
} | ||
map2[str]++; | ||
str=""; | ||
|
||
for(auto i:map2){ | ||
string sen = i.first; | ||
int n1 = i.second; | ||
if(map1[sen]) map1[sen] += n1; | ||
else map1[sen]= n1; | ||
} | ||
for(auto i:map1) if(i.second==1) ans.push_back(i.first); | ||
// for(auto i:map1)ans.push_back(i.first); | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Longest valid Parentheses | ||
Given a string str consisting of opening and closing parenthesis '(' and ')'. Find length of the longest valid parenthesis substring. | ||
A parenthesis string is valid if: | ||
For every opening parenthesis, there is a closing parenthesis. | ||
Opening parenthesis must be closed in the correct order. | ||
Examples : | ||
Input: str = ((() | ||
Output: 2 | ||
Explaination: The longest valid parenthesis substring is "()". | ||
Input: str = )()()) | ||
Output: 4 | ||
Explaination: The longest valid parenthesis substring is "()()". | ||
Expected Time Complexity: O(|str|) | ||
Expected Auxiliary Space: O(|str|) | ||
Constraints: | ||
1 ≤ |str| ≤ 105 | ||
*/ | ||
// User function Template for C++ | ||
|
||
class Solution { | ||
public: | ||
int maxLength(string& str) { | ||
// code here | ||
int n = str.size(); | ||
stack<int> st; | ||
int ans = 0; | ||
for(int i=0; i<n; i++) | ||
{ | ||
if(str[i] == '(') | ||
{ | ||
st.push(i); | ||
} | ||
else | ||
{ | ||
if(!st.empty() && str[st.top()] == '(') | ||
{ | ||
st.pop(); | ||
int first = st.empty() ? -1 : st.top(); | ||
ans = max(ans,i-first); | ||
} | ||
else | ||
{ | ||
st.push(i); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
539. Minimum Time Difference | ||
Given a list of 24-hour clock time points in "HH:MM" format, return the minimum minutes difference between any two time-points in the list. | ||
Example 1: | ||
Input: timePoints = ["23:59","00:00"] | ||
Output: 1 | ||
Example 2: | ||
Input: timePoints = ["00:00","23:59","00:00"] | ||
Output: 0 | ||
Constraints: | ||
2 <= timePoints.length <= 2 * 104 | ||
timePoints[i] is in the format "HH:MM". | ||
*/ | ||
class Solution { | ||
public: | ||
int findMinDifference(vector<string>& timePoints) { | ||
vector<int> minutes(timePoints.size()); | ||
|
||
// Convert times to minutes | ||
for (int i = 0; i < timePoints.size(); ++i) { | ||
int h = stoi(timePoints[i].substr(0, 2)); | ||
int m = stoi(timePoints[i].substr(3)); | ||
minutes[i] = h * 60 + m; | ||
} | ||
|
||
// Sort times in ascending order | ||
sort(minutes.begin(), minutes.end()); | ||
|
||
// Find minimum difference across adjacent elements | ||
int minDiff = INT_MAX; | ||
for (int i = 0; i < minutes.size() - 1; ++i) { | ||
minDiff = min(minDiff, minutes[i + 1] - minutes[i]); | ||
} | ||
|
||
// Consider the circular difference between last and first element | ||
minDiff = min(minDiff, 24 * 60 - minutes.back() + minutes.front()); | ||
|
||
return minDiff; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Longest valid Parentheses | ||
Given a string str consisting of opening and closing parenthesis '(' and ')'. Find length of the longest valid parenthesis substring. | ||
A parenthesis string is valid if: | ||
For every opening parenthesis, there is a closing parenthesis. | ||
Opening parenthesis must be closed in the correct order. | ||
Examples : | ||
Input: str = ((() | ||
Output: 2 | ||
Explaination: The longest valid parenthesis substring is "()". | ||
Input: str = )()()) | ||
Output: 4 | ||
Explaination: The longest valid parenthesis substring is "()()". | ||
Expected Time Complexity: O(|str|) | ||
Expected Auxiliary Space: O(|str|) | ||
Constraints: | ||
1 ≤ |str| ≤ 105 | ||
*/ | ||
// User function Template for C++ | ||
|
||
class Solution { | ||
public: | ||
int maxLength(string& str) { | ||
// code here | ||
int n = str.size(); | ||
stack<int> st; | ||
int ans = 0; | ||
for(int i=0; i<n; i++) | ||
{ | ||
if(str[i] == '(') | ||
{ | ||
st.push(i); | ||
} | ||
else | ||
{ | ||
if(!st.empty() && str[st.top()] == '(') | ||
{ | ||
st.pop(); | ||
int first = st.empty() ? -1 : st.top(); | ||
ans = max(ans,i-first); | ||
} | ||
else | ||
{ | ||
st.push(i); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Minimize the Heights II | ||
Given an array arr[] denoting heights of N towers and a positive integer K. | ||
For each tower, you must perform exactly one of the following operations exactly once. | ||
Increase the height of the tower by K | ||
Decrease the height of the tower by K | ||
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower. | ||
You can find a slight modification of the problem here. | ||
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers. | ||
Examples : | ||
Input: k = 2, arr[] = {1, 5, 8, 10} | ||
Output: 5 | ||
Explanation: The array can be modified as {1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.The difference between the largest and the smallest is 8-3 = 5. | ||
Input: k = 3, arr[] = {3, 9, 12, 16, 20} | ||
Output: 11 | ||
Explanation: The array can be modified as {3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.The difference between the largest and the smallest is 17-6 = 11. | ||
Expected Time Complexity: O(n*logn) | ||
Expected Auxiliary Space: O(n) | ||
Constraints | ||
1 ≤ k ≤ 107 | ||
1 ≤ n ≤ 105 | ||
1 ≤ arr[i] ≤ 107 | ||
*/ | ||
// User function template for C++ | ||
|
||
class Solution { | ||
public: | ||
int getMinDiff(vector<int> &arr, int k) { | ||
// code here | ||
int n = arr.size(); | ||
sort(arr.begin(),arr.end()); | ||
int min_diff = arr[n-1] - arr[0]; | ||
for(int i = 1; i < n; i++){ | ||
if(arr[i] < k) continue; | ||
int mini = min(arr[0] + k, arr[i] - k); | ||
int maxi = max(arr[n-1] - k, arr[i-1] + k); | ||
min_diff = min(min_diff, maxi - mini); | ||
} | ||
return min_diff; | ||
} | ||
}; |
50 changes: 50 additions & 0 deletions
50
25_Dynamic_Programming/Problems_on_dp/Hard/Longest_valid_Parentheses.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Longest valid Parentheses | ||
Given a string str consisting of opening and closing parenthesis '(' and ')'. Find length of the longest valid parenthesis substring. | ||
A parenthesis string is valid if: | ||
For every opening parenthesis, there is a closing parenthesis. | ||
Opening parenthesis must be closed in the correct order. | ||
Examples : | ||
Input: str = ((() | ||
Output: 2 | ||
Explaination: The longest valid parenthesis substring is "()". | ||
Input: str = )()()) | ||
Output: 4 | ||
Explaination: The longest valid parenthesis substring is "()()". | ||
Expected Time Complexity: O(|str|) | ||
Expected Auxiliary Space: O(|str|) | ||
Constraints: | ||
1 ≤ |str| ≤ 105 | ||
*/ | ||
// User function Template for C++ | ||
|
||
class Solution { | ||
public: | ||
int maxLength(string& str) { | ||
// code here | ||
int n = str.size(); | ||
stack<int> st; | ||
int ans = 0; | ||
for(int i=0; i<n; i++) | ||
{ | ||
if(str[i] == '(') | ||
{ | ||
st.push(i); | ||
} | ||
else | ||
{ | ||
if(!st.empty() && str[st.top()] == '(') | ||
{ | ||
st.pop(); | ||
int first = st.empty() ? -1 : st.top(); | ||
ans = max(ans,i-first); | ||
} | ||
else | ||
{ | ||
st.push(i); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Longest valid Parentheses | ||
Given a string str consisting of opening and closing parenthesis '(' and ')'. Find length of the longest valid parenthesis substring. | ||
A parenthesis string is valid if: | ||
For every opening parenthesis, there is a closing parenthesis. | ||
Opening parenthesis must be closed in the correct order. | ||
Examples : | ||
Input: str = ((() | ||
Output: 2 | ||
Explaination: The longest valid parenthesis substring is "()". | ||
Input: str = )()()) | ||
Output: 4 | ||
Explaination: The longest valid parenthesis substring is "()()". | ||
Expected Time Complexity: O(|str|) | ||
Expected Auxiliary Space: O(|str|) | ||
Constraints: | ||
1 ≤ |str| ≤ 105 | ||
*/ | ||
// User function Template for C++ | ||
|
||
class Solution { | ||
public: | ||
int maxLength(string& str) { | ||
// code here | ||
int n = str.size(); | ||
stack<int> st; | ||
int ans = 0; | ||
for(int i=0; i<n; i++) | ||
{ | ||
if(str[i] == '(') | ||
{ | ||
st.push(i); | ||
} | ||
else | ||
{ | ||
if(!st.empty() && str[st.top()] == '(') | ||
{ | ||
st.pop(); | ||
int first = st.empty() ? -1 : st.top(); | ||
ans = max(ans,i-first); | ||
} | ||
else | ||
{ | ||
st.push(i); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
Minimize the Heights II | ||
Given an array arr[] denoting heights of N towers and a positive integer K. | ||
For each tower, you must perform exactly one of the following operations exactly once. | ||
Increase the height of the tower by K | ||
Decrease the height of the tower by K | ||
Find out the minimum possible difference between the height of the shortest and tallest towers after you have modified each tower. | ||
You can find a slight modification of the problem here. | ||
Note: It is compulsory to increase or decrease the height by K for each tower. After the operation, the resultant array should not contain any negative integers. | ||
Examples : | ||
Input: k = 2, arr[] = {1, 5, 8, 10} | ||
Output: 5 | ||
Explanation: The array can be modified as {1+k, 5-k, 8-k, 10-k} = {3, 3, 6, 8}.The difference between the largest and the smallest is 8-3 = 5. | ||
Input: k = 3, arr[] = {3, 9, 12, 16, 20} | ||
Output: 11 | ||
Explanation: The array can be modified as {3+k, 9+k, 12-k, 16-k, 20-k} -> {6, 12, 9, 13, 17}.The difference between the largest and the smallest is 17-6 = 11. | ||
Expected Time Complexity: O(n*logn) | ||
Expected Auxiliary Space: O(n) | ||
Constraints | ||
1 ≤ k ≤ 107 | ||
1 ≤ n ≤ 105 | ||
1 ≤ arr[i] ≤ 107 | ||
*/ | ||
// User function template for C++ | ||
|
||
class Solution { | ||
public: | ||
int getMinDiff(vector<int> &arr, int k) { | ||
// code here | ||
int n = arr.size(); | ||
sort(arr.begin(),arr.end()); | ||
int min_diff = arr[n-1] - arr[0]; | ||
for(int i = 1; i < n; i++){ | ||
if(arr[i] < k) continue; | ||
int mini = min(arr[0] + k, arr[i] - k); | ||
int maxi = max(arr[n-1] - k, arr[i-1] + k); | ||
min_diff = min(min_diff, maxi - mini); | ||
} | ||
return min_diff; | ||
} | ||
}; |
Oops, something went wrong.