diff --git a/08_String/Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp b/08_String/Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp new file mode 100644 index 0000000..22534be --- /dev/null +++ b/08_String/Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp @@ -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 uncommonFromSentences(string s1, string s2) { + vector ans; + map 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; + } +}; \ No newline at end of file diff --git a/08_String/Problems/Hard/Longest_valid_Parentheses.cpp b/08_String/Problems/Hard/Longest_valid_Parentheses.cpp new file mode 100644 index 0000000..b7bf22f --- /dev/null +++ b/08_String/Problems/Hard/Longest_valid_Parentheses.cpp @@ -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 st; + int ans = 0; + for(int i=0; i& timePoints) { + vector 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; + } +}; \ No newline at end of file diff --git a/13_Stack/Problems/Hard/Longest_valid_Parentheses.cpp b/13_Stack/Problems/Hard/Longest_valid_Parentheses.cpp new file mode 100644 index 0000000..b7bf22f --- /dev/null +++ b/13_Stack/Problems/Hard/Longest_valid_Parentheses.cpp @@ -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 st; + int ans = 0; + for(int i=0; i {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 &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; + } +}; \ No newline at end of file diff --git a/25_Dynamic_Programming/Problems_on_dp/Hard/Longest_valid_Parentheses.cpp b/25_Dynamic_Programming/Problems_on_dp/Hard/Longest_valid_Parentheses.cpp new file mode 100644 index 0000000..b7bf22f --- /dev/null +++ b/25_Dynamic_Programming/Problems_on_dp/Hard/Longest_valid_Parentheses.cpp @@ -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 st; + int ans = 0; + for(int i=0; i st; + int ans = 0; + for(int i=0; i {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 &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; + } +}; \ No newline at end of file diff --git a/Leet Code Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp b/Leet Code Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp new file mode 100644 index 0000000..22534be --- /dev/null +++ b/Leet Code Problems/Easy/884_Uncommon_Words_from_Two_Sentences.cpp @@ -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 uncommonFromSentences(string s1, string s2) { + vector ans; + map 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; + } +}; \ No newline at end of file diff --git a/Leet Code Problems/Medium/539_Minimum_Time_Difference.cpp b/Leet Code Problems/Medium/539_Minimum_Time_Difference.cpp new file mode 100644 index 0000000..79f93d6 --- /dev/null +++ b/Leet Code Problems/Medium/539_Minimum_Time_Difference.cpp @@ -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& timePoints) { + vector 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; + } +}; \ No newline at end of file