New Problem Solution -"Longest Nice Substring"
This commit is contained in:
parent
b80be4327a
commit
424dec7e28
@ -12,6 +12,7 @@ LeetCode
|
||||
|1786|[Number of Restricted Paths From First to Last Node](https://leetcode.com/problems/number-of-restricted-paths-from-first-to-last-node/) | [C++](./algorithms/cpp/numberOfRestrictedPathsFromFirstToLastNode/NumberOfRestrictedPathsFromFirstToLastNode.cpp)|Medium|
|
||||
|1785|[Minimum Elements to Add to Form a Given Sum](https://leetcode.com/problems/minimum-elements-to-add-to-form-a-given-sum/) | [C++](./algorithms/cpp/minimumElementsToAddToFormAGivenSum/MinimumElementsToAddToFormAGivenSum.cpp)|Medium|
|
||||
|1784|[Check if Binary String Has at Most One Segment of Ones](https://leetcode.com/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [C++](./algorithms/cpp/checkIfBinaryStringHasAtMostOneSegmentOfOnes/CheckIfBinaryStringHasAtMostOneSegmentOfOnes.cpp)|Easy|
|
||||
|1763|[Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [C++](./algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp)|Easy|
|
||||
|1761|[Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/) | [C++](./algorithms/cpp/minimumDegreeOfAConnectedTrioInAGraph/MinimumDegreeOfAConnectedTrioInAGraph.cpp)|Hard|
|
||||
|1760|[Minimum Limit of Balls in a Bag](https://leetcode.com/problems/minimum-limit-of-balls-in-a-bag/) | [C++](./algorithms/cpp/minimumLimitOfBallsInABag/MinimumLimitOfBallsInABag.cpp)|Medium|
|
||||
|1759|[Count Number of Homogenous Substrings](https://leetcode.com/problems/count-number-of-homogenous-substrings/) | [C++](./algorithms/cpp/countNumberOfHomogenousSubstrings/CountNumberOfHomogenousSubstrings.cpp)|Medium|
|
||||
|
79
algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp
Normal file
79
algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
// Source : https://leetcode.com/problems/longest-nice-substring/
|
||||
// Author : Hao Chen
|
||||
// Date : 2021-03-08
|
||||
|
||||
/*****************************************************************************************************
|
||||
*
|
||||
* A string s is nice if, for every letter of the alphabet that s contains, it appears both in
|
||||
* uppercase and lowercase. For example, "abABB" is nice because 'A' and 'a' appear, and 'B' and 'b'
|
||||
* appear. However, "abA" is not because 'b' appears, but 'B' does not.
|
||||
*
|
||||
* Given a string s, return the longest substring of s that is nice. If there are multiple, return the
|
||||
* substring of the earliest occurrence. If there are none, return an empty string.
|
||||
*
|
||||
* Example 1:
|
||||
*
|
||||
* Input: s = "YazaAay"
|
||||
* Output: "aAa"
|
||||
* Explanation: "aAa" is a nice string because 'A/a' is the only letter of the alphabet in s, and both
|
||||
* 'A' and 'a' appear.
|
||||
* "aAa" is the longest nice substring.
|
||||
*
|
||||
* Example 2:
|
||||
*
|
||||
* Input: s = "Bb"
|
||||
* Output: "Bb"
|
||||
* Explanation: "Bb" is a nice string because both 'B' and 'b' appear. The whole string is a substring.
|
||||
*
|
||||
* Example 3:
|
||||
*
|
||||
* Input: s = "c"
|
||||
* Output: ""
|
||||
* Explanation: There are no nice substrings.
|
||||
*
|
||||
* Example 4:
|
||||
*
|
||||
* Input: s = "dDzeE"
|
||||
* Output: "dD"
|
||||
* Explanation: Both "dD" and "eE" are the longest nice substrings.
|
||||
* As there are multiple longest nice substrings, return "dD" since it occurs earlier.
|
||||
*
|
||||
* Constraints:
|
||||
*
|
||||
* 1 <= s.length <= 100
|
||||
* s consists of uppercase and lowercase English letters.
|
||||
******************************************************************************************************/
|
||||
|
||||
class Solution {
|
||||
inline int getCharIndex(char c) {
|
||||
return (c >='A' && c <='Z') ? c - 'A' : c - 'a';
|
||||
}
|
||||
inline int getCaseIndex(char c) {
|
||||
return (c >='A' && c <='Z') ? 1 : 0;
|
||||
}
|
||||
public:
|
||||
string longestNiceSubstring(string s) {
|
||||
vector<bitset<26>> check(2);
|
||||
int start = 0, len = 0;
|
||||
for (int i = 0; i < s.size() -1; i++){
|
||||
for (int j = i+1; j < s.size(); j++) {
|
||||
|
||||
check[0] = check[1] = 0;
|
||||
for (int x=i; x<=j; x++){
|
||||
int i = getCaseIndex(s[x]);
|
||||
int j = getCharIndex(s[x]);
|
||||
check[i][j] = true;
|
||||
}
|
||||
|
||||
if ( (check[0] ^ check[1]) == 0 ) {
|
||||
if ( j - i + 1 > len ){
|
||||
start = i;
|
||||
len = j-i+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return s.substr(start, len);
|
||||
}
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user