{"id":8127,"date":"2021-02-20T15:29:06","date_gmt":"2021-02-20T23:29:06","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8127"},"modified":"2021-02-20T15:29:44","modified_gmt":"2021-02-20T23:29:44","slug":"leetcode-1763-longest-nice-substring","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1763-longest-nice-substring\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1763. Longest Nice Substring"},"content":{"rendered":"\n<p>A string&nbsp;<code>s<\/code>&nbsp;is&nbsp;<strong>nice<\/strong>&nbsp;if, for every letter of the alphabet that&nbsp;<code>s<\/code>&nbsp;contains, it appears&nbsp;<strong>both<\/strong>&nbsp;in uppercase and lowercase. For example,&nbsp;<code>\"abABB\"<\/code>&nbsp;is nice because&nbsp;<code>'A'<\/code>&nbsp;and&nbsp;<code>'a'<\/code>&nbsp;appear, and&nbsp;<code>'B'<\/code>&nbsp;and&nbsp;<code>'b'<\/code>&nbsp;appear. However,&nbsp;<code>\"abA\"<\/code>&nbsp;is not because&nbsp;<code>'b'<\/code>&nbsp;appears, but&nbsp;<code>'B'<\/code>&nbsp;does not.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<em>the longest&nbsp;<strong>substring<\/strong>&nbsp;of&nbsp;<code>s<\/code>&nbsp;that is&nbsp;<strong>nice<\/strong>. If there are multiple, return the substring of the&nbsp;<strong>earliest<\/strong>&nbsp;occurrence. If there are none, return an empty string<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"YazaAay\"\n<strong>Output:<\/strong> \"aAa\"\n<strong>Explanation: <\/strong>\"aAa\" is a nice string because 'A\/a' is the only letter of the alphabet in s, and both 'A' and 'a' appear.\n\"aAa\" is the longest nice substring.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"Bb\"\n<strong>Output:<\/strong> \"Bb\"\n<strong>Explanation:<\/strong> \"Bb\" is a nice string because both 'B' and 'b' appear. The whole string is a substring.<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"c\"\n<strong>Output:<\/strong> \"\"\n<strong>Explanation:<\/strong> There are no nice substrings.<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"dDzeE\"\n<strong>Output:<\/strong> \"dD\"\n<strong>Explanation: <\/strong>Both \"dD\" and \"eE\" are the longest nice substrings.\nAs there are multiple longest nice substrings, return \"dD\" since it occurs earlier.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;consists of uppercase and lowercase English letters.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n^3)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string longestNiceSubstring(string_view s) {    \n    const int n = s.length();\n    auto isNice = [](string_view s) {\n      vector<int> u(26), l(26);\n      for (int c : s)\n        if (isupper(c)) u[c - 'A'] = 1;\n        else l[c - 'a'] = 1;\n      return u == l;\n    };\n    string_view ans;    \n    for (int i = 0; i < n; ++i)\n      for (int j = i + 1; j < n; ++j) {\n        string_view ss = s.substr(i, j - i + 1);\n        if (isNice(ss) &#038;&#038; ss.length() > ans.length())\n          ans = ss;\n      }\n    return string(ans);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Optimized 1: <\/p>\n\n\n\n<p>Time complexity: O(n^2*26)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  string longestNiceSubstring(string_view s) {    \n    const int n = s.length();    \n    string_view ans;\n    for (int i = 0; i < n; ++i) {\n      vector<int> u(26), l(26);\n      for (int j = i; j < n; ++j) {\n        const char c = s[j];\n        if (isupper(c)) u[c - 'A'] = 1;\n        else l[c - 'a'] = 1;\n        if (u == l &#038;&#038; j - i + 1 > ans.length())\n          ans = s.substr(i, j - i + 1);\n      }\n    }\n    return string(ans);\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A string&nbsp;s&nbsp;is&nbsp;nice&nbsp;if, for every letter of the alphabet that&nbsp;s&nbsp;contains, it appears&nbsp;both&nbsp;in uppercase and lowercase. For example,&nbsp;&#8220;abABB&#8221;&nbsp;is nice because&nbsp;&#8216;A&#8217;&nbsp;and&nbsp;&#8216;a&#8217;&nbsp;appear, and&nbsp;&#8216;B&#8217;&nbsp;and&nbsp;&#8216;b&#8217;&nbsp;appear. However,&nbsp;&#8220;abA&#8221;&nbsp;is not because&nbsp;&#8216;b&#8217;&nbsp;appears, but&nbsp;&#8216;B&#8217;&nbsp;does not. Given&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-8127","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8127"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8127\/revisions"}],"predecessor-version":[{"id":8129,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8127\/revisions\/8129"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}