id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\n int findMiniDel(vector<int> &a_count, vector<int> &b_sum, string &reduced_s, int a_pos, int b_pos) {\n if(a_pos<0 || b_pos<0) {\n return 0;\n }\n // findMiniDel(a_count, b_sum, string &reduced_s, pos-1)\n int delete_a = a_count[a_pos] + findMiniDel(a_count, b_sum, reduced_s, a_pos-1, b_pos-1);\n int donot_delete_a = b_sum[b_pos];\n return min(delete_a, donot_delete_a);\n }\npublic:\n int minimumDeletions(string s) {\n vector<int> a_count, b_sum;\n string reduced_s;\n char current_char=s[0];\n int cur_counter=1, last_num;\n if(s[0] == 'b') {\n reduced_s.push_back(s[0]);\n }\n \n for(int i=1; i<s.length(); i++) {\n if(current_char == s[i]) {\n cur_counter++;\n } else {\n \n \n if(current_char == 'a') {\n // if(a_sum.size()==0) {\n // last_num = 0;\n // } else {\n // last_num = a_count[a_count.size()-1];\n // }\n if(reduced_s != \"\") {\n // cout<<\"\\n pushing\"\n a_count.push_back(cur_counter);\n }\n \n \n } else {\n // cout<<\"\\nfound b\\n\";\n if(b_sum.size()==0) {\n last_num = 0;\n } else {\n last_num = b_sum[b_sum.size()-1];\n }\n b_sum.push_back(last_num+cur_counter);\n\n }\n cur_counter = 1;\n current_char = s[i];\n reduced_s.push_back(s[i]);\n }\n }\n if(current_char == 'a') {\n a_count.push_back(cur_counter);\n }\n\n\n // if(reduced_s[reduced_s.length()-1] == 'b') {\n // reduced_s.pop_back();\n // b_sum.pop_back();\n // }\n\n\n // cout<<\"\\nreduced s: \"<<reduced_s;\n // cout<<\"\\na_counts: \";\n // for(auto x:a_count) {\n // cout<<x<<\" \";\n // }\n // cout<<\"\\nb_sums: \";\n // for(auto x:b_sum) {\n // cout<<x<<\" \";\n // }\n return findMiniDel(a_count, b_sum, reduced_s, a_count.size()-1, b_sum.size()-1);\n \n }\n};", "memory": "50470" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\n int findMiniDel(vector<int> &a_count, vector<int> &b_sum, string &reduced_s, int a_pos, int b_pos) {\n if(a_pos<0 || b_pos<0) {\n return 0;\n }\n // findMiniDel(a_count, b_sum, string &reduced_s, pos-1)\n int delete_a = a_count[a_pos] + findMiniDel(a_count, b_sum, reduced_s, a_pos-1, b_pos-1);\n int donot_delete_a = b_sum[b_pos];\n return min(delete_a, donot_delete_a);\n }\npublic:\n int minimumDeletions(string s) {\n vector<int> a_count, b_sum;\n string reduced_s;\n char current_char=s[0];\n int cur_counter=1, last_num;\n if(s[0] == 'b') {\n reduced_s.push_back(s[0]);\n }\n \n for(int i=1; i<s.length(); i++) {\n if(current_char == s[i]) {\n cur_counter++;\n } else {\n \n \n if(current_char == 'a') {\n // if(a_sum.size()==0) {\n // last_num = 0;\n // } else {\n // last_num = a_count[a_count.size()-1];\n // }\n if(reduced_s != \"\") {\n // cout<<\"\\n pushing\"\n a_count.push_back(cur_counter);\n }\n \n \n } else {\n // cout<<\"\\nfound b\\n\";\n if(b_sum.size()==0) {\n last_num = 0;\n } else {\n last_num = b_sum[b_sum.size()-1];\n }\n b_sum.push_back(last_num+cur_counter);\n\n }\n cur_counter = 1;\n current_char = s[i];\n reduced_s.push_back(s[i]);\n }\n }\n if(current_char == 'a') {\n a_count.push_back(cur_counter);\n }\n\n\n // if(reduced_s[reduced_s.length()-1] == 'b') {\n // reduced_s.pop_back();\n // b_sum.pop_back();\n // }\n\n\n // cout<<\"\\nreduced s: \"<<reduced_s;\n // cout<<\"\\na_counts: \";\n // for(auto x:a_count) {\n // cout<<x<<\" \";\n // }\n // cout<<\"\\nb_sums: \";\n // for(auto x:b_sum) {\n // cout<<x<<\" \";\n // }\n return findMiniDel(a_count, b_sum, reduced_s, a_count.size()-1, b_sum.size()-1);\n \n }\n};", "memory": "50890" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> compressed;\n int cnt = 0;\n int i = 0;\n while(i<s.size()){\n int left = i;\n ++i;\n while(i<s.size()&&s[i]==s[i-1]){\n ++i;\n }\n cnt = i - left;\n compressed.push_back(cnt);\n }\n\n i = 0*(s[0]=='b')+1*(s[0]=='a');\n cnt = 0;\n vector<int> dp(compressed.size());\n for(int j = i+1;j<compressed.size();j+=2){\n cnt+=compressed[j-1];\n dp[j] = cnt;\n }\n for(int j = i+2;j<dp.size();j+=2){\n dp[j] = min( dp[j-1], compressed[j-1] + dp[j-2] );\n }\n if( s[ s.size() - 1] == 'a'&&dp.size()>2){\n dp[dp.size()-2] += compressed[compressed.size()-1];\n return min( dp[dp.size()-1],dp[dp.size()-2] );\n }\n return dp[dp.size()-1];\n }\n};", "memory": "50890" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> v;\n char c = s.front();\n int cnt = 0;\n for (char ch : s) {\n if (ch == c) ++cnt;\n else {\n c = ch;\n v.push_back(cnt);\n cnt = 1;\n }\n }\n v.push_back(cnt);\n\n vector<int> cost(v.size());\n cnt = 0;\n for (int i = s[0] == 'a'; i < cost.size(); i += 2) {\n cost[i] = cnt;\n cnt += v[i];\n }\n\n int ans = cnt;\n cnt = 0;\n bool is_b = s.back() == 'b';\n for (int i = v.size() - 1; i >= 0; i--) {\n if (is_b) ans = min(ans, cnt + cost[i]);\n else cnt += v[i];\n is_b = not is_b;\n }\n if(is_b) ans = min(ans, cnt);\n\n return ans;\n }\n};", "memory": "51310" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution \n{\n vector<pair<int,int>> preprocess(string const & s)\n {\n vector<pair<int,int>> excludes;\n int idx = 0;\n while (idx<s.size() && s[idx]=='a')\n {\n ++idx;\n }\n pair<int,int> exclude;\n while(idx<s.size())\n {\n while (idx < s.size() && s[idx]=='b')\n {\n exclude.first++;\n ++idx;\n }\n while (idx < s.size() && s[idx]=='a')\n {\n exclude.second++;\n ++idx;\n } \n excludes.push_back(exclude); \n exclude = {0,0};\n }\n if (!excludes.empty() && excludes.back().second==0)\n excludes.pop_back();\n return excludes;\n }\npublic:\n int minimumDeletions(string s) \n {\n auto excludes = preprocess(s);\n if (excludes.empty())\n return 0;\n int n = excludes.size(), opt = INT_MAX;\n vector<pair<int, int>> prefix(excludes.size()+1);\n for (int i = 0; i<n; ++i)\n {\n prefix[i+1].first = prefix[i].first + excludes[i].first;\n }\n opt = prefix.back().first;\n for (int i = n-1; i>=0; --i)\n {\n prefix[i].second = prefix[i+1].second + excludes[i].second;\n opt = min(opt, prefix[i].first + prefix[i].second);\n }\n return opt;\n }\n};", "memory": "51310" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> cost;\n\n int bCount = s[0] == 'b';\n bool seenA = false;\n for (int i = 1; i < s.size() - 1; ++i)\n {\n seenA = seenA || s[i] == 'a';\n\n if (s[i] != s[i + 1] || s[i] != s[i - 1])\n {\n cost.push_back(seenA ? bCount : 0);\n }\n\n bCount += s[i] == 'b';\n }\n\n if (cost.size() == 0)\n {\n return 0;\n }\n\n int aCount = s.back() == 'a';\n bool seenB = false;\n int j = cost.size() - 1;\n int minn = INT_MAX;\n for (int i = s.size() - 2; i >= 1; --i)\n {\n seenB = seenB || s[i] == 'b';\n\n if (s[i] != s[i-1] || s[i] != s[i+1])\n {\n if (seenB)\n cost[j] += aCount;\n minn = min(minn, cost[j]);\n j--;\n }\n\n aCount += s[i] == 'a';\n }\n\n return minn;\n }\n};", "memory": "51730" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n // count (aa..a...bb..b) set\n vector<int> num_a, num_b;\n vector<pair<int, int>> dp;\n int i = 0;\n int n = s.length();\n\n while(i < n){\n int a = 0;\n int b = 0;\n while(i < n && s[i] == 'a'){\n a++;\n i++;\n }\n while(i < n && s[i] == 'b'){\n b++;\n i++;\n }\n num_a.push_back(a);\n num_b.push_back(b);\n }\n\n dp.resize(num_a.size()+1, {0, 0});\n for(int j=0; j<dp.size()-1; j++){\n dp[j+1].second = dp[j].second + num_b[j];\n dp[dp.size()-2-j].first = dp[dp.size()-1-j].first + num_a[dp.size()-2-j];\n }\n int min_sum = INT_MAX;\n for(int i=0;i<dp.size()-1; i++){\n min_sum = min(min_sum, dp[i+1].first + dp[i].second);\n }\n return min_sum;\n }\n};", "memory": "52150" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n vector<pair<int, int>> vec(n);\n vec[0].first = 0;\n for(int i = 1; i < n; i++) {\n if(s[i-1] == 'b') vec[i].first = vec[i-1].first + 1;\n else vec[i].first = vec[i-1].first;\n } \n \n vec[n-1].second = 0;\n int mini = vec[n-1].second + vec[n-1].first;\n for(int i = n-2; i >= 0; i--) {\n if(s[i+1] == 'a') vec[i].second = vec[i+1].second + 1;\n else vec[i].second = vec[i+1].second;\n mini = min(mini, vec[i].first + vec[i].second);\n }\n\n return mini;\n }\n};", "memory": "52150" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n vector<pair<int, int>> counts(n + 1, {0, 0});\n int count = 0, ans = INT_MAX;\n\n for(int i = 0; i < n; ++i) {\n if(s[i] == 'b') {\n ++count;\n }\n\n counts[i + 1].first = count;\n }\n\n count = 0;\n\n for(int i = n - 1; i >= 0; --i) {\n if(s[i] == 'a') {\n ++count;\n }\n\n counts[i].second = count;\n }\n\n for(int i = 0; i < n + 1; ++i) {\n ans = min(ans, counts[i].first + counts[i].second);\n }\n\n return ans;\n }\n};", "memory": "52570" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n int n = s.size();\n if(n==1) {\n return 0;\n }\n\n vector<pair<int,int>>dp(n, pair<int,int>{0,0});\n\n int countA = 0, countB = 0;\n char a = 'a', b = 'b';\n\n for(int i=0;i<n;i++) {\n if(s[i]==a)\n countA++;\n else\n countB++;\n\n dp[i].first = countA;\n dp[i].second = countB;\n }\n\n // cout<<\"==================\"<<endl;\n // for(int i=0;i<n;i++) {\n // cout<<s[i]<<\" \";\n // }\n // cout<<endl;\n // for(int i=0;i<n;i++) {\n // cout<<dp[i].first<<\" \";\n // }\n // cout<<endl;\n // for(int i=0;i<n;i++) {\n // cout<<dp[i].second<<\" \";\n // }\n // cout<<endl;\n // cout<<\"==================\"<<endl;\n\n int ans = n;\n //keep current character\n for(int i=0;i<n;i++) {\n int curAns = 0;\n\n if(s[i]==a) {\n curAns += ( (i>0 ? dp[i-1].second : 0) + dp[n-1].first - dp[i].first);\n } else {\n curAns += ( (i>0 ? dp[i-1].first : 0) + dp[n-1].first - dp[i].first);\n }\n // cout<<\"##############\"<<endl;\n // cout<<curAns<<endl;\n // cout<<\"##############\"<<endl;\n ans = min(ans, curAns);\n }\n return ans;\n }\n};", "memory": "52570" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string a) {\n int n=a.size(),i,ans=n,x,y;\n vector<int> pre(n),post(n);\n pre[0]=(a[0]=='b'?1:0);\n post[n-1]=(a[n-1]=='a'?1:0);\n for(i=1;i<n;i++)\n pre[i]=pre[i-1]+(a[i]=='b'?1:0);\n for(i=n-2;i>=0;i--)\n post[i]=post[i+1]+(a[i]=='a'?1:0);\n ans=min(ans,post[0]);\n for(i=0;i<n;i++)\n {\n x=pre[i];\n y=0;\n if(i+1<n)\n y=post[i+1];\n ans=min(ans,x+y);\n }\n return ans;\n }\n};", "memory": "52990" }
1,756
<p>You are given a string <code>s</code> consisting only of characters <code>&#39;a&#39;</code> and <code>&#39;b&#39;</code>​​​​.</p> <p>You can delete any number of characters in <code>s</code> to make <code>s</code> <strong>balanced</strong>. <code>s</code> is <strong>balanced</strong> if there is no pair of indices <code>(i,j)</code> such that <code>i &lt; j</code> and <code>s[i] = &#39;b&#39;</code> and <code>s[j]= &#39;a&#39;</code>.</p> <p>Return <em>the <strong>minimum</strong> number of deletions needed to make </em><code>s</code><em> <strong>balanced</strong></em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;aababbab&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> You can either: Delete the characters at 0-indexed positions 2 and 6 (&quot;aa<u>b</u>abb<u>a</u>b&quot; -&gt; &quot;aaabbb&quot;), or Delete the characters at 0-indexed positions 3 and 6 (&quot;aab<u>a</u>bb<u>a</u>b&quot; -&gt; &quot;aabbbb&quot;). </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;bbaaaaabb&quot; <strong>Output:</strong> 2 <strong>Explanation:</strong> The only solution is to delete the first two characters. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li> <li><code>s[i]</code> is&nbsp;<code>&#39;a&#39;</code> or <code>&#39;b&#39;</code>​​.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int minimumDeletions(string s) {\n vector<int> prefix_b(s.size() + 2, 0), suffix_a(s.size() + 2, 0);\n prefix_b[0] = 0;\n suffix_a[suffix_a.size() - 1] = 0;\n\n for(int i = 1; i < prefix_b.size() - 1; i++) {\n if(s[i - 1] == 'b') {\n prefix_b[i + 1] = prefix_b[i] + 1;\n } else {\n prefix_b[i + 1] = prefix_b[i] + 0;\n }\n }\n\n for(int i = s.size(); i >= 1; i--) {\n if(s[i - 1] == 'a') {\n suffix_a[i - 1] = suffix_a[i] + 1;\n } else {\n suffix_a[i - 1] = suffix_a[i] + 0;\n }\n }\n\n int mini = INT_MAX;\n\n for(int i = 0; i < s.size() + 2; i++) {\n mini = min(prefix_b[i] + suffix_a[i], mini);\n }\n\n\n return mini;\n }\n};", "memory": "52990" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tstd::array<int, 26> getLetterCount(const string &word)\n\t{\n\t\tstd::array<int, 26> arr = { 0 };\n\n\t\tfor (std::size_t i = 0; i < word.size(); ++i)\n\t\t{\n\t\t\tarr[static_cast<char>(word[i]) - 97] ++;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\tbool closeStrings(const string &word1, const string &word2)\n\t{\n\t\tstd::array<int, 26> w1LetterCount = getLetterCount(word1);\n\t\tstd::array<int, 26> w2LetterCount = getLetterCount(word2);\n\n\t\tfor (std::size_t i = 0; i < w1LetterCount.size(); ++i)\n\t\t{\n\t\t\tif (w1LetterCount[i] > 0 && w2LetterCount[i] == 0 ||\n\t\t\t\tw1LetterCount[i] == 0 && w2LetterCount[i] > 0)\n\t\t\t\treturn false;\n\t\t}\n\n\t\tstd::multiset<int> w1Set;\n\t\tstd::multiset<int> w2Set;\n\t\tfor (auto& item : w1LetterCount) w1Set.insert(item);\n\t\tfor (auto& item : w2LetterCount) w2Set.insert(item);\n\n\t\treturn w1Set == w2Set;\n\t}\n};", "memory": "19000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\tstd::array<int, 26> getLetterCount(const string &word)\n\t{\n\t\tstd::array<int, 26> arr = { 0 };\n\n\t\tfor (std::size_t i = 0; i < word.size(); ++i)\n\t\t{\n\t\t\tarr[static_cast<char>(word[i]) - 97] ++;\n\t\t}\n\n\t\treturn arr;\n\t}\n\n\tbool closeStrings(const string &word1, const string &word2)\n\t{\n\t\tstd::array<int, 26> w1LetterCount = getLetterCount(word1);\n\t\tstd::array<int, 26> w2LetterCount = getLetterCount(word2);\n\n\t\tfor (std::size_t i = 0; i < w1LetterCount.size(); ++i)\n\t\t{\n\t\t\tif (w1LetterCount[i] > 0 && w2LetterCount[i] == 0 ||\n\t\t\t\tw1LetterCount[i] == 0 && w2LetterCount[i] > 0)\n\t\t\t\treturn false;\n\t\t}\n\n\t\tstd::multiset<int> w1Set;\n\t\tstd::multiset<int> w2Set;\n\t\tfor (auto& item : w1LetterCount) w1Set.insert(item);\n\t\tfor (auto& item : w2LetterCount) w2Set.insert(item);\n\n\t\treturn w1Set == w2Set;\n\t}\n};", "memory": "19100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n Solution() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n }\n\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size())\n return false;\n\n\n std::vector<int> s1(256, 0);\n std::vector<int> s2(256, 0);\n\n std::for_each(word1.begin(), word1.end(), [&s1](const char c) { s1[c]++; });\n std::for_each(word2.begin(), word2.end(), [&s2](const char c) { s2[c]++; });\n\n for (int i = 0; i < 256; ++i) {\n if (s1[i] == 0 && s2[i] != 0)\n return false;\n else if (s2[i] == 0 && s1[i] != 0)\n return false;\n }\n\n std::sort(s1.begin(), s1.end());\n std::sort(s2.begin(), s2.end());\n\n return s1 == s2;\n\n }\n};", "memory": "19300" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string& word1, string& word2) \n {\n if (word1.size() != word2.size())\n return false;\n \n vector<int> map1(26, 0);\n vector<int> map2(26, 0);\n\n unordered_set<char> word1_set;\n unordered_set<char> word2_set;\n\n for (int i = 0; i < word1.size(); i++)\n {\n //cout << int(word1[i] - 'a') << endl;\n map1[int(word1[i] - 'a')] += 1;\n map2[int(word2[i] - 'a')] += 1;\n\n word1_set.insert(word1[i]);\n word2_set.insert(word2[i]);\n }\n\n if (word1_set != word2_set)\n return false;\n \n sort(map1.begin(), map1.end());\n sort(map2.begin(), map2.end());\n return (map1 == map2);\n }\n};", "memory": "19800" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n unordered_map<int, int> get_histo(const string& s) const\n {\n unordered_map<int, int> m;\n\n for (const char c : s)\n {\n ++m[c];\n }\n\n return m;\n }\n bool closeStrings(string& word1, const string& word2) {\n // we can only work with the existing character set\n\n if (word1.size() != word2.size())\n {\n return false;\n }\n\n // operation 1 means that the order isn't important\n // could take any char anywhere, and move it elsewhere\n // this means that two words with equivalent letter histograms are close\n\n // the second property means that we can swap any two counts\n\n auto m1{get_histo(word1)};\n auto m2{get_histo(word2)};\n\n if (m1.size() != m2.size())\n return false;\n\n for (const auto& p : m1)\n {\n if (m2.find(p.first)==m2.cend())\n {\n return false;\n }\n }\n\n\n // we want to see if we can make m1 equal m2, just by moving counts around - what if we just sort the counts and see if they match...\n\n vector<int> c1{};\n vector<int> c2{};\n\n transform(m1.cbegin(), m1.cend(), back_inserter(c1), [](const auto& p){return p.second;});\n\n transform(m2.cbegin(), m2.cend(), back_inserter(c2), [](const auto& p){return p.second;});\n\n sort(c1.begin(), c1.end());\n sort(c2.begin(), c2.end());\n\n return c1 == c2;\n\n }\n};", "memory": "19900" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.length() != word2.length()) {\n return false;\n }\n\n std::unordered_map<char, int> freq1, freq2;\n \n for (char c : word1) {\n freq1[c]++;\n }\n \n for (char c : word2) {\n freq2[c]++;\n }\n \n // Check if all characters in word1 are in word2 and vice versa\n if (freq1.size() != freq2.size()) {\n return false;\n }\n \n for (const auto& pair : freq1) {\n if (freq2.find(pair.first) == freq2.end()) {\n return false;\n }\n }\n \n // Check if frequency counts are the same\n std::vector<int> freq1Values, freq2Values;\n for (const auto& pair : freq1) {\n freq1Values.push_back(pair.second);\n }\n for (const auto& pair : freq2) {\n freq2Values.push_back(pair.second);\n }\n \n std::sort(freq1Values.begin(), freq1Values.end());\n std::sort(freq2Values.begin(), freq2Values.end());\n \n return freq1Values == freq2Values;\n }\n};\n", "memory": "20000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "#include <vector>\n#include <algorithm>\n#include <string>\n#include <unordered_map>\n\nclass Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n // If lengths are different, they can't be close\n if (word1.size() != word2.size()) return false;\n\n // Frequency maps for both strings\n std::unordered_map<char, int> freq1, freq2;\n for (char c : word1) freq1[c]++;\n for (char c : word2) freq2[c]++;\n\n // Check if both strings use the same set of characters\n if (freq1.size() != freq2.size()) return false;\n for (auto& entry : freq1) {\n if (freq2.find(entry.first) == freq2.end()) return false;\n }\n\n // Compare frequency distributions\n std::vector<int> counts1, counts2;\n for (auto& entry : freq1) counts1.push_back(entry.second);\n for (auto& entry : freq2) counts2.push_back(entry.second);\n\n std::sort(counts1.begin(), counts1.end());\n std::sort(counts2.begin(), counts2.end());\n\n return counts1 == counts2;\n }\n};\n", "memory": "20000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n\n map<int, int> get_histo(const string& s) const\n {\n map<int, int> m;\n\n for (const char c : s)\n {\n ++m[c];\n }\n\n return m;\n }\n bool closeStrings(string& word1, const string& word2) {\n // we can only work with the existing character set\n\n if (word1.size() != word2.size())\n {\n return false;\n }\n\n // operation 1 means that the order isn't important\n // could take any char anywhere, and move it elsewhere\n // this means that two words with equivalent letter histograms are close\n\n // the second property means that we can swap any two counts\n\n auto m1{get_histo(word1)};\n auto m2{get_histo(word2)};\n\n if (m1.size() != m2.size())\n return false;\n\n for (const auto& p : m1)\n {\n if (m2.find(p.first)==m2.cend())\n {\n return false;\n }\n }\n\n\n // we want to see if we can make m1 equal m2, just by moving counts around - what if we just sort the counts and see if they match...\n\n vector<int> c1{};\n vector<int> c2{};\n\n transform(m1.cbegin(), m1.cend(), back_inserter(c1), [](const auto& p){return p.second;});\n\n transform(m2.cbegin(), m2.cend(), back_inserter(c2), [](const auto& p){return p.second;});\n\n sort(c1.begin(), c1.end());\n sort(c2.begin(), c2.end());\n\n return c1 == c2;\n\n }\n};", "memory": "20100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string& word1, string& word2) {\n if(word1.size() != word2.size())\n return false;\n sort(word1.begin(), word1.end());\n sort(word2.begin(), word2.end());\n if(word1 == word2) return true;\n unordered_map<char, int> count1, count2;\n unordered_set<int> mems1, mems2;\n vector<int> freqs1, freqs2;\n for(int i=0; i<word1.size(); ++i) {\n ++count1[word1[i]];\n ++count2[word2[i]];\n mems1.insert(word1[i]);\n mems2.insert(word2[i]);\n }\n for(const auto& [_, count]: count1)\n freqs1.push_back(count);\n for(const auto& [_, count]: count2) \n freqs2.push_back(count);\n sort(freqs1.begin(), freqs1.end());\n sort(freqs2.begin(), freqs2.end());\n return mems1 == mems2 && freqs1 == freqs2;\n }\n};", "memory": "20200" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::unordered_map<char, int> map1, map2;\n std::unordered_set<char> set1, set2;\n\n // Populate the frequency maps and character sets\n for (const char& c : word1) {\n map1[c]++;\n set1.insert(c);\n }\n\n for (const char& c : word2) {\n map2[c]++;\n set2.insert(c);\n }\n\n // Check if both character sets are identical\n if (set1 != set2) {\n return false;\n }\n\n // Extract and sort frequency counts\n std::vector<int> freq1, freq2;\n\n for (const auto& [key, value] : map1) {\n freq1.push_back(value);\n }\n\n for (const auto& [key, value] : map2) {\n freq2.push_back(value);\n }\n\n std::sort(freq1.begin(), freq1.end());\n std::sort(freq2.begin(), freq2.end());\n\n // Compare sorted frequency vectors\n return freq1 == freq2;\n }\n};\n\n", "memory": "20300" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::unordered_map<char, int> map1, map2;\n std::unordered_set<char> set1, set2;\n\n // Populate the frequency maps and character sets\n for (const char& c : word1) {\n map1[c]++;\n set1.insert(c);\n }\n\n for (const char& c : word2) {\n map2[c]++;\n set2.insert(c);\n }\n\n // Check if both character sets are identical\n if (set1 != set2) {\n return false;\n }\n\n // Extract and sort frequency counts\n std::vector<int> freq1, freq2;\n\n for (const auto& [key, value] : map1) {\n freq1.push_back(value);\n }\n\n for (const auto& [key, value] : map2) {\n freq2.push_back(value);\n }\n\n std::sort(freq1.begin(), freq1.end());\n std::sort(freq2.begin(), freq2.end());\n\n // Compare sorted frequency vectors\n return freq1 == freq2;\n }\n};\n\n", "memory": "20400" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string &word1, string &word2) {\n unordered_set<char> w1(word1.begin(), word1.end()), w2(word2.begin(), word2.end());\n if (w1 != w2) return false;\n unordered_map<char, int> mp1, mp2; // {char -> frequency}\n for (char c : word1) ++mp1[c];\n for (char c : word2) ++mp2[c];\n unordered_multiset<int> freq1, freq2;\n for (auto& [_, f] : mp1) freq1.insert(f);\n for (auto& [_, f] : mp2) freq2.insert(f);\n return freq1 == freq2;\n }\n};", "memory": "20500" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string &word1, string &word2) {\n unordered_map<char, int> mp1, mp2; // {char -> frequency}\n for (char c : word1) ++mp1[c];\n for (char c : word2) ++mp2[c];\n unordered_multiset<int> freq1, freq2;\n unordered_set<char> w1, w2;\n for (auto& [c, f] : mp1) {\n w1.insert(c);\n freq1.insert(f);\n }\n for (auto& [c, f] : mp2) {\n w2.insert(c);\n freq2.insert(f);\n }\n return w1 == w2 && freq1 == freq2;\n }\n};\n// we only need to make sure: 1. the two words use the same set of letters; 2. the multiset of frequencies of the letters in both strings are the same", "memory": "20500" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(const std::string& word1, const std::string& word2) {\n // If the lengths are different, they cannot be close\n if (word1.size() != word2.size()) {\n return false;\n }\n\n std::map<char, int> word1Map;\n std::map<char, int> word2Map;\n\n // Count the frequency of each character in word1 and word2\n for (char character : word1) {\n word1Map[character]++;\n }\n for (char character : word2) {\n word2Map[character]++;\n }\n\n // Check if the sets of characters are the same\n std::unordered_set<char> set1;\n std::unordered_set<char> set2;\n for (const auto& pair : word1Map) {\n set1.insert(pair.first);\n }\n for (const auto& pair : word2Map) {\n set2.insert(pair.first);\n }\n if (set1 != set2) {\n return false;\n }\n\n // Collect and sort the frequencies\n std::vector<int> word1Freq;\n std::vector<int> word2Freq;\n\n for (const auto& pair : word1Map) {\n word1Freq.push_back(pair.second);\n }\n for (const auto& pair : word2Map) {\n word2Freq.push_back(pair.second);\n }\n\n std::sort(word1Freq.begin(), word1Freq.end());\n std::sort(word2Freq.begin(), word2Freq.end());\n\n // Compare sorted frequency vectors directly\n return word1Freq == word2Freq;\n }\n};\n", "memory": "20700" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \n void mergeSort(int low, int mid, int high, vector<int>& vec){\n if(low == high){\n return;\n }\n mergeSort(low, (low+mid)/2, mid, vec);\n mergeSort(mid+1, (mid+1+high)/2, high, vec);\n vector<int> v1(mid-low+1), v2(high - mid);\n int i = 0, j = 0;\n for(int k = low; k <= mid; k++){\n v1[i] = vec[k];\n i++;\n }\n for(int l = mid+1; l <= high; l++){\n v2[j] = vec[l];\n j++;\n }\n i = 0, j = 0;\n while(i < v1.size() && j < v2.size()){\n if(v1[i] >= v2[j]){\n vec[low] = v2[j];\n low++;\n j++;\n }else{\n vec[low] = v1[i];\n low++;\n i++;\n }\n }\n while(i < v1.size()){\n vec[low] = v1[i];\n i++;\n low++;\n }\n while(j < v2.size()){\n vec[low] = v2[j];\n j++;\n low++;\n }\n}\nbool closeStrings(string& s1, string & s2){\n vector<int> v1(27, 0), v2(27, 0);\n for(auto a: s1){\n v1[a - 'a']++;\n }for(auto a: s2){\n v2[a - 'a']++;\n }\n for(int i = 0; i < v1.size(); i++){\n if((v1[i] == 0 && v2[i] != 0) || (v1[i] != 0 && v2[i] == 0)){\n return false;\n }\n }\n mergeSort(0, (v1.size()-1)/2, v1.size()-1, v1);\n mergeSort(0, (v2.size()-1)/2, v2.size()-1, v2);\n \n for(int i = 0; i < v1.size(); i++){\n if(v1[i] != v2[i]){\n return false;\n }\n }\n return true;\n}\n\n \n};", "memory": "21200" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n // if(word1.length()!=word2.length()) return false;\n // unordered_map<char,char>m1,m2;\n // for(int i=0;i<word1.length();i++){\n // m1[word1[i]]++;\n // m2[word2[i]]++;\n\n\n // }\n // unordered_map<int,int>h1,h2;\n // for(auto x:m1){\n // char ch=x.first;\n // if(m2.find(ch)==m2.end()){\n // return false;\n\n // }\n\n // }\n // for(auto x:m2){\n // int freq=x.second;\n // h2[freq]++;\n // }\n // for(auto x:m1){\n // int freq=x.second;\n // h1[freq]++;\n // }\n // //comparing helper 1 and helper 2 maps\n // for(auto x:h1){\n // int key=x.first;\n // if(h2.find(key)==h2.end()) return false;\n // if(h2[key]!=h1[key]) return false;\n\n // }\n // return true;\n int m=word1.length();\n int n=word2.length();\n if(m!=n) return false;\n vector<int>freq1(26);\n vector<int>freq2(26);\n for(int i=0;i<m;i++){\n char ch1=word1[i];\n char ch2=word2[i];\n int idx1=ch1-'a';\n int idx2=ch2-'a';\n freq1[idx1]++;\n freq2[idx2]++;\n }\n for(int i=0;i<26;i++){\n if(freq1[i]!=0 && freq2[i]!=0) continue;\n if(freq1[i]==0 && freq2[i]==0 ) continue;\n return false;\n\n }\n sort(begin(freq1),end(freq1));\n sort(begin(freq2),end(freq2));\n \n \n return freq1==freq2;;\n\n\n }\n};", "memory": "21700" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int m = word1.length(); \n int n = word2.length(); \n\n \n if(m != n)\n return false;\n\n vector<int> frequency1(26); \n vector<int> frequency2(26); \n\n for(int i = 0; i < m; ++i){\n char ch1 = word1[i]; \n char ch2 = word2[i]; \n\n int index1 = ch1 - 'a'; \n int index2 = ch2 - 'a'; \n\n frequency1[index1]++; \n frequency2[index2]++; \n\n }\n\n for(int i = 0; i < 26; i++){\n if(frequency1[i] != 0 && frequency2[i] != 0) continue; \n if(frequency1[i] == 0 && frequency2[i] == 0) continue; \n\n return false;\n }\n\n \n sort(frequency1.begin(), frequency1.end());\n sort(frequency2.begin(), frequency2.end());\n\n return frequency1 == frequency2;\n \n }\n};", "memory": "21800" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n = word1.length();\n int m = word2.length();\n\n vector<int> freq1(26, 0);\n vector<int> freq2(26, 0);\n\n for(auto c: word1){\n freq1[c-'a']++;\n }\n for(auto c: word2){\n freq2[c-'a']++;\n }\n\n for(int i=0; i<26; i++){\n if(freq1[i]>0 && freq2[i]==0) return false;\n if(freq2[i]>0 && freq1[i]==0) return false;\n }\n sort(freq1.begin(), freq1.end());\n sort(freq2.begin(), freq2.end());\n\n for(int i=0; i<26; i++){\n if(freq1[i]!=freq2[i]) return false;\n }\n\n return true;\n }\n};", "memory": "21900" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(std::string word1, std::string word2) {\n std::vector<int> freq1(26, 0);\n std::vector<int> freq2(26, 0);\n\n for (char ch : word1) {\n freq1[ch - 'a']++;\n }\n\n for (char ch : word2) {\n freq2[ch - 'a']++;\n }\n\n for (int i = 0; i < 26; i++) {\n if ((freq1[i] == 0 && freq2[i] != 0) || (freq1[i] != 0 && freq2[i] == 0)) {\n return false;\n }\n }\n\n std::sort(freq1.begin(), freq1.end());\n std::sort(freq2.begin(), freq2.end());\n\n for (int i = 0; i < 26; i++) {\n if (freq1[i] != freq2[i]) {\n return false;\n }\n }\n\n return true;\n }\n};\n\n", "memory": "22000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> w1(26,0), w2(26,0);\n if(word1.size()!=word2.size()) return false;\n for(char ch: word1)\n w1[ch-'a']++;\n for(char ch: word2)\n w2[ch-'a']++;\n for(int i=0; i<26; i++){\n if(w1[i]==0 && w2[i]!=0 || w1[i]!=0 && w2[i]==0) return false;\n }\n sort(w1.begin(),w1.end());\n sort(w2.begin(),w2.end());\n return w1==w2;\n }\n};", "memory": "22000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> v1(26, 0);\n vector<int> v2(26, 0);\n for(int i=0; i<word1.length(); i++) v1[word1[i]-'a']++;\n for(int i=0; i<word2.length(); i++) v2[word2[i]-'a']++;\n for(int i=0; i<26; i++){\n if(v1[i]==0 ^ v2[i]==0) return false;\n }\n sort(v1.begin(), v1.end());\n sort(v2.begin(), v2.end());\n return v1==v2;\n }\n};", "memory": "22100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
0
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int m = word1.length(); // length of 1st string word1...\n int n = word2.length(); // length of 2nd string word2...\n\n //if both strings aren't of same length then, returns false...\n if(m != n)\n return false;\n\n vector<int> frequency1(26); // for track frequency of characters in word1...\n vector<int> frequency2(26); // for track frequency of characters in word2...\n\n for(int i = 0; i < m; ++i){\n\n frequency1[word1[i]-'a']++; \n frequency2[word2[i]-'a']++; \n\n }\n\n for(int i = 0; i < 26; i++){\n if(frequency1[i] != 0 && frequency2[i] != 0) continue; \n if(frequency1[i] == 0 && frequency2[i] == 0) continue; \n\n return false;\n }\n sort(frequency1.begin(), frequency1.end());\n sort(frequency2.begin(), frequency2.end());\n\n return frequency1 == frequency2;\n }\n};", "memory": "22100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool closeStrings(string s1, string s2) {\n int n=s1.size(),m=s2.size();\n vector<char>a(26,0),b(26,0);\n for(int i=0;i<n;i++)\n {\n int x=s1[i]-'a';\n a[x]++;\n }\n for(int i=0;i<m;i++)\n b[s2[i]-'a']++;\n sort(a.rbegin(),a.rend());\n sort(b.rbegin(),b.rend());\n for(int i=0;i<26;i++){\n if(a[i]!=b[i]) return 0;\n else {\n char ch=97+i;\n if(s1.find(ch)!=-1 && s2.find(ch)==-1 )\n return 0;\n else if(s1.find(ch)==-1 && s2.find(ch)!=-1)\n return 0;\n }\n }\n return 1;\n }\n};", "memory": "22800" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int a[256];\n int b[256];\n bool closeStrings(string w1, string w2) {\n if (w1.size() != w2.size())\n return false;\n \n for (int i = 0; i < w1.size(); i++) {\n a[(int)w1[i]]++;\n b[(int)w2[i]]++;\n }\n\n for (int i = 0; i < 256; i++) {\n if (a[i] > 0 && b[i] == 0)\n return false;\n if (a[i] == 0 && b[i] > 0)\n return false;\n }\n\n sort(a, a + 256);\n sort(b, b + 256);\n\n for (int i = 0; i < 256; i++) {\n if (a[i] != b[i])\n return false;\n }\n return true;\n }\n};", "memory": "22900" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.length() != word2.length()) return false;\n\n vector<int> cnts1(26);\n vector<int> cnts2(26);\n for(char c : word1) cnts1[c - 'a']++;\n for(char c : word2) cnts2[c - 'a']++;\n\n unordered_map<int, int> cnts;\n vector<int> inds(26);\n for(int i = 0; i < 26; i++) {\n cnts[cnts1[i]]++;\n if(cnts1[i]) inds[i]++;\n }\n for(int i = 0; i < 26; i++) {\n if(cnts2[i] && !inds[i]) return false;\n cnts[cnts2[i]]--;\n if(cnts[cnts2[i]] < 0) return false;\n }\n return true;\n }\n};", "memory": "23000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()==word2.size()){\n int mp1[256]={0};\n int mp2[256]={0};\n\n set<char>st1;\n set<char>st2;\n\n for(int i=0;i<word1.size();i++){\n mp1[word1[i]]++;\n st1.insert(word1[i]);\n }\n\n for(int i=0;i<word2.size();i++){\n mp2[word2[i]]++;\n st2.insert(word2[i]);\n }\n \n if(st1!=st2){\n return false;\n }\n\n sort(mp1,mp1+256,greater<int>());\n sort(mp2,mp2+256,greater<int>());\n\n return equal(begin(mp1), end(mp1), begin(mp2));\n\n } \n\n return false; \n }\n};", "memory": "23100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n vector<int> f1(26,0),f2=f1;\n unordered_set<int> hs;\n for(char c:word1){\n hs.insert(c);\n f1[c-'a']++;\n }\n for(char c:word2){\n if(hs.find(c)==hs.end()){\n return 0;\n }\n f2[c-'a']++;\n }\n sort(f1.begin(),f1.end());\n sort(f2.begin(),f2.end());\n return f1==f2;\n }\n};", "memory": "23200" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
1
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int a1[26],a2[26];\n for(int i=0;i<word1.size();i++) a1[word1[i]-'a']++;\n for(int i=0;i<word2.size();i++) a2[word2[i]-'a']++;\n map<int,int> m;\n for(int i=0;i<26;i++) m[a1[i]]++;\n for(int i=0;i<26;i++) m[a2[i]]--;\n\n for(int i=0;i<26;i++) if((a1[i] && !a2[i]) || (!a1[i] && a2[i])) return 0;\n for(auto it : m) if(it.second) return 0;\n return 1;\n }\n};", "memory": "23300" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n \n if(word1.size() != word2.size()) return false;\n\n unordered_map<int,int> freq1,freq2;\n for(char c: word1){\n freq1[c]++;\n }\n\n for(char c: word2){\n freq2[c]++;\n }\n\n for(char c: word1){\n if(freq2.find(c) == freq2.end()) return false;\n }\n\n for(char c:word2){\n if(freq1.find(c) == freq1.end()) return false;\n }\n\n vector<int> uni1,uni2;\n for(auto it:freq1){\n uni1.push_back(it.second);\n }\n\n for(auto it:freq2){\n uni2.push_back(it.second);\n }\n sort(uni1.begin(),uni1.end());\n sort(uni2.begin(),uni2.end());\n \n return uni1==uni2;\n }\n};", "memory": "23400" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2)\n {\n unordered_map<char, int> map1;\n unordered_map<char, int> map2;\n unordered_map<int, int> freq;\n\n //alog:\n //len should be the same\n //both string should have same letters (no same freq)\n //freq should be same\n\n if(word1.size() != word2.size())\n {\n return false;\n }\n\n for(int i = 0; i < word1.size(); i++)\n {\n map1[word1[i]]++;\n }\n\n for(int i = 0; i < word2.size(); i++)\n {\n map2[word2[i]]++;\n }\n\n //make sure that the letter are the same\n for(auto it=map1.begin(); it != map1.end(); it++)\n {\n if(map2[it->first] == 0)\n {\n return false;\n }\n\n freq[it->second]++; \n }\n\n //make sure that the freq are the same\n for(auto it=map2.begin(); it != map2.end(); it++)\n {\n if(freq[it->second] == 0)\n {\n return false;\n }\n\n freq[it->second]--; \n }\n\n return true;\n }\n};", "memory": "23400" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!= word2.size())return 0; \n unordered_map<char, int>map1;\n unordered_map<char, int>map2;\n multiset<int> multiSet1;\n multiset<int> multiSet2;\n for(int i=0; i<word1.size(); i++){\n map1[word1[i]]++;\n map2[word2[i]]++;\n }\n\n for(const auto& i : map1) {\n if(!map2.count(i.first))return 0; \n multiSet1.insert(i.second);\n } \n\n for(const auto& i : map2) {\n if(!map1.count(i.first))return 0; \n multiSet2.insert(i.second);\n } \n \n return multiSet1==multiSet2 ? 1 : 0; \n }\n};", "memory": "23500" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n const int n1 = word1.size();\n const int n2 = word2.size();\n if (n1 != n2)\n return false;\n sort(word1.begin(), word1.end());\n sort(word2.begin(), word2.end());\n if (word1 == word2)\n return true;\n unordered_map<int, int> dict1{};\n unordered_map<int, int> dict2{};\n for (const char w: word1)\n ++ dict1[w];\n for (const char w: word2)\n ++ dict2[w];\n for (const auto& [c, _]: dict1)\n {\n if (!dict2.count(c))\n return false;\n }\n for (const auto& [c, _]: dict2)\n {\n if (!dict1.count(c))\n return false;\n }\n vector<int> v1;\n vector<int> v2;\n for (const auto& [_, cnt]: dict1)\n {\n v1.push_back(cnt);\n }\n for (const auto& [_, cnt]: dict2)\n {\n v2.push_back(cnt);\n }\n sort(v1.begin(), v1.end());\n sort(v2.begin(), v2.end());\n return v1 == v2;\n }\n};", "memory": "23500" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n1 = word1.size();\n int n2 = word2.size();\n\n if(n1 != n2) return false;\n\n unordered_map<char, int> mp1;\n unordered_map<char, int> mp2;\n\n set<char> st1;\n set<char> st2;\n\n for(int i = 0; i < n1; i++) {\n mp1[word1[i]]++;\n st1.insert(word1[i]);\n }\n for(int i = 0; i < n2; i++){\n mp2[word2[i]]++;\n st2.insert(word2[i]);\n }\n if(st1 != st2) return false;\n\n vector<int> nums1;\n vector<int> nums2;\n\n for(auto ele : mp1) nums1.push_back(ele.second);\n for(auto ele : mp2) nums2.push_back(ele.second);\n\n sort(nums1.begin(), nums1.end());\n sort(nums2.begin(), nums2.end());\n\n if(nums1.size() != nums2.size()) return false;\n\n for(int i = 0; i < nums1.size(); i++) {\n if(nums1[i] != nums2[i]) return false;\n }\n\n return true;\n\n\n }\n};", "memory": "23600" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n\n if(word1.length()!=word2.length()){\n return false;\n }\n unordered_map<char , int>freq1;\n unordered_map<char , int>freq2;\n for(char ch : word1){\n freq1[ch]++;\n }\n for(char ch : word2){\n freq2[ch]++;\n }\n\n if(freq1.size() != freq2.size()){\n return false;\n }\n for(const auto& [key , value]:freq1 ){\n if(freq2.find(key)==freq2.end()){\n return false;\n }\n }\n\n unordered_map<int , int>freqCount1;\n unordered_map<int , int>freqCount2;\n\n for(const auto&[key,freq]:freq1){\n freqCount1[freq]++;\n }\n for(const auto& [key,freq] : freq2){\n freqCount2[freq]++;\n }\n return freqCount1==freqCount2;\n }\n};", "memory": "23600" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n\n bool closeStrings(string word1, string word2) {\n int n=word1.length();\n int m=word2.length();\n if(n!=m) return false;\n map<char,int> m1,m2;\n vector<int> v1,v2;\n for(int i=0;i<n;i++){\n m1[word1[i]]++;\n }\n for(int i=0;i<m;i++){\n if(m1.find(word2[i])==m1.end()) return false;\n m2[word2[i]]++;\n }\n for(auto it:m1){\n v1.push_back(it.second);\n }\n for(auto it:m2){\n v2.push_back(it.second);\n }\n sort(v1.begin(),v1.end());\n sort(v2.begin(),v2.end());\n if(v1==v2) return true;\n return false;\n }\n};", "memory": "23700" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
2
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!=word2.size()) \n return false;\n unordered_map<char,int>mp1;\n unordered_map<char,int>mp2;\n for(auto x:word1)\n {\n mp1[x]++;\n } \n for(auto x:word2)\n {\n mp2[x]++;\n } \n if(mp1.size()!=mp2.size()) return false;\n vector<int>al1;vector<int>nu1;vector<int>al2;vector<int>nu2;\n for(auto x:mp1)\n {\n al1.push_back(x.first);\n nu1.push_back(x.second);\n }\n for(auto x:mp2)\n {\n al2.push_back(x.first);\n nu2.push_back(x.second);\n }\n sort(al1.begin(),al1.end());\n sort(al2.begin(),al2.end());\n sort(nu1.begin(),nu1.end());\n sort(nu2.begin(),nu2.end());\n for(int i=0;i<al1.size();i++)\n {\n if(al1[i]!=al2[i])\n return false;\n }\n for(int i=0;i<nu1.size();i++)\n {\n if(nu1[i]!=nu2[i])\n return false;\n }\n return true;\n \n \n\n\n }\n};", "memory": "23700" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!= word2.size())return 0; \n unordered_map<char, int>map1;\n unordered_map<char, int>map2;\n multiset<int> multiSet1;\n multiset<int> multiSet2;\n for(int i=0; i<word1.size(); i++){\n map1[word1[i]]++;\n map2[word2[i]]++;\n }\n\n for(const auto& i : map1) {\n if(!map2.count(i.first))return 0; \n multiSet1.insert(i.second);\n } \n\n for(const auto& i : map2) {\n if(!map1.count(i.first))return 0; \n multiSet2.insert(i.second);\n } \n \n return multiSet1==multiSet2 ? 1 : 0; \n }\n};", "memory": "23800" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n unordered_set<char> s;\n unordered_map<char, int> mp;\n for(int i=0; i<word1.length(); i++){\n s.insert(word1[i]);\n mp[word1[i]]++;\n }\n unordered_map<char, int> mp2;\n for(int i=0; i<word2.length(); i++){\n if(s.find(word2[i])==s.end()){\n return false;\n }\n mp2[word2[i]]++;\n }\n multiset<int, greater<int>> s1;\n multiset<int, greater<int>> s2;\n for(auto i:mp){\n s1.insert(i.second);\n }\n for(auto i:mp2){\n s2.insert(i.second);\n }\n if(s1!=s2) return false;\n return true;\n }\n};", "memory": "23800" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n1 = word1.size();\n int n2 = word2.size();\n\n if(n1 != n2) return false;\n\n unordered_map<char, int> mp1;\n unordered_map<char, int> mp2;\n\n set<char> st1;\n set<char> st2;\n\n for(int i = 0; i < n1; i++) {\n mp1[word1[i]]++;\n st1.insert(word1[i]);\n }\n for(int i = 0; i < n2; i++){\n mp2[word2[i]]++;\n st2.insert(word2[i]);\n }\n if(st1 != st2) return false;\n\n vector<int> nums1;\n vector<int> nums2;\n\n for(auto ele : mp1) nums1.push_back(ele.second);\n for(auto ele : mp2) nums2.push_back(ele.second);\n\n sort(nums1.begin(), nums1.end());\n sort(nums2.begin(), nums2.end());\n\n if(nums1.size() != nums2.size()) return false;\n\n for(int i = 0; i < nums1.size(); i++) {\n if(nums1[i] != nums2[i]) return false;\n }\n\n return true;\n\n\n }\n};", "memory": "23900" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n map<char,int>m;\n map<char,int>m2;\n vector<int>v;\n vector<int>v2;\n set<char>ss;\n for(auto i:word1) ss.insert(i);\n for(auto i:word2) if(ss.count(i)==0) return false;\n\n for(auto i:word1){\n m[i]++;\n }\n for(auto i:word2){\n m2[i]++;\n }\n for(auto it:m){\n v.push_back(it.second);\n }\n for(auto it:m2){\n v2.push_back(it.second);\n }\n sort(v.begin(),v.end());\n sort(v2.begin(),v2.end());\n return v==v2;\n }\n};", "memory": "23900" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "#include <ranges>\n\nclass Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n unordered_map<char, int> hist1, hist2;\n for(const char e: word1)\n hist1[e]++;\n\n for(const char e: word2)\n hist2[e]++;\n\n\n auto vit1 = views::values(hist1);\n multiset<int> vals1(vit1.begin(), vit1.end());\n\n auto vit2 = views::values(hist2);\n multiset<int> vals2(vit2.begin(), vit2.end());\n\n auto kit1 = views::keys(hist1);\n unordered_set<char> keys1(kit1.begin(), kit1.end());\n\n auto kit2 = views::keys(hist2);\n unordered_set<char> keys2(kit2.begin(), kit2.end());\n\n return vals1==vals2 and keys1==keys2;\n }\n};", "memory": "24000" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n int n=word1.size();\n int m=word2.size();\n\n if(n!=m){\n return false;\n }\n\n map<char,int>m1;\n map<char,int>m2;\n\n for(int i=0;i<word1.length();i++){\n m1[word1[i]]++;\n }\n\n for(int i=0;i<word2.length();i++){\n m2[word2[i]]++;\n }\n\n set<char> set1;\n set<char> set2;\n\n for (auto i: m1) {\n set1.insert(i.first);\n }\n for (auto i: m2) {\n set2.insert(i.first);\n }\n \n\n if (set1 != set2) {\n return false;\n }\n\n\n vector<int>vec1;\n vector<int>vec2;\n\n for(auto i:m1){\n vec1.push_back(i.second);\n }\n for(auto i:m2){\n vec2.push_back(i.second);\n }\n sort(vec1.begin(),vec1.end());\n sort(vec2.begin(),vec2.end());\n\n for(int i=0;i<vec1.size();i++){\n if(vec1[i]!=vec2[i]){\n return false;\n }\n }\n return true;\n\n }\n};", "memory": "24100" }
1,777
<p>Two strings are considered <strong>close</strong> if you can attain one from the other using the following operations:</p> <ul> <li>Operation 1: Swap any two <strong>existing</strong> characters. <ul> <li>For example, <code>a<u>b</u>cd<u>e</u> -&gt; a<u>e</u>cd<u>b</u></code></li> </ul> </li> <li>Operation 2: Transform <strong>every</strong> occurrence of one <strong>existing</strong> character into another <strong>existing</strong> character, and do the same with the other character. <ul> <li>For example, <code><u>aa</u>c<u>abb</u> -&gt; <u>bb</u>c<u>baa</u></code> (all <code>a</code>&#39;s turn into <code>b</code>&#39;s, and all <code>b</code>&#39;s turn into <code>a</code>&#39;s)</li> </ul> </li> </ul> <p>You can use the operations on either string as many times as necessary.</p> <p>Given two strings, <code>word1</code> and <code>word2</code>, return <code>true</code><em> if </em><code>word1</code><em> and </em><code>word2</code><em> are <strong>close</strong>, and </em><code>false</code><em> otherwise.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;abc&quot;, word2 = &quot;bca&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 2 operations. Apply Operation 1: &quot;a<u>bc</u>&quot; -&gt; &quot;a<u>cb</u>&quot; Apply Operation 1: &quot;<u>a</u>c<u>b</u>&quot; -&gt; &quot;<u>b</u>c<u>a</u>&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;a&quot;, word2 = &quot;aa&quot; <strong>Output:</strong> false <strong>Explanation: </strong>It is impossible to attain word2 from word1, or vice versa, in any number of operations. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> word1 = &quot;cabbba&quot;, word2 = &quot;abbccc&quot; <strong>Output:</strong> true <strong>Explanation:</strong> You can attain word2 from word1 in 3 operations. Apply Operation 1: &quot;ca<u>b</u>bb<u>a</u>&quot; -&gt; &quot;ca<u>a</u>bb<u>b</u>&quot; Apply Operation 2: &quot;<u>c</u>aa<u>bbb</u>&quot; -&gt; &quot;<u>b</u>aa<u>ccc</u>&quot; Apply Operation 2: &quot;<u>baa</u>ccc&quot; -&gt; &quot;<u>abb</u>ccc&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= word1.length, word2.length &lt;= 10<sup>5</sup></code></li> <li><code>word1</code> and <code>word2</code> contain only lowercase English letters.</li> </ul>
3
{ "code": "class Solution {\npublic:\n bool closeStrings(string word1, string word2) {\n if(word1.size()!=word2.size()){\n return false;\n }\n set<char> words1,words2;\n for(auto c:word1){\n words1.insert(c);\n }\n for(auto c:word2){\n words2.insert(c);\n }\n if(words1!=words2){\n return false;\n }\n map<char,int> count1,count2;\n map<int,int> count_1,count_2;\n for(auto it:word1){\n count1[it]++;\n }\n for(auto it:word2){\n count2[it]++;\n }\n for(auto it = count1.begin();it!=count1.end();it++){\n count_1[it->second]++;\n }\n for(auto it = count2.begin();it!=count2.end();it++){\n count_2[it->second]++;\n }\n for(auto it = count_1.begin();it!=count_1.end();it++){\n if(count_1[it->first]!=count_2[it->first]){\n return false;\n }\n }\n \n \n\n\n return true;\n \n }\n};", "memory": "24100" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n \n int n = nums.size();\n vector<int> pre(n);\n vector<int> suf(n);\n pre[0] = nums[0];\n suf[n-1] = nums[n-1];\n for(int i = 1;i<n;i++){\n pre[i] = pre[i-1] + nums[i];\n suf[n-i-1] = suf[n-i] + nums[n-i-1];\n }\n\n int ans = 1e9;\n\n for(int i = n-1;i>=0;i--){\n int k = x-suf[i];\n auto it = lower_bound(pre.begin(), pre.end(), k);\n if(it<=(pre.begin()+i-1) && (*it) == k){\n cout<<*it<<endl;\n int y = (it-pre.begin())+1;\n ans = min(ans,(n-i+y));\n }\n }\n\n auto it1 = lower_bound(pre.begin(),pre.end(),x);\n\n if(it1!=pre.end() && (*it1)==x){\n int y = (it1-pre.begin());\n ans = min(ans,y+1);\n }\n\n reverse(suf.begin(),suf.end());\n\n auto it2 = lower_bound(suf.begin(),suf.end(),x);\n\n if(it2!=suf.end() && (*it2)==x){\n int y = (it2-suf.begin());\n ans = min(ans,y+1);\n }\n\n if(ans>=1e9){\n return -1;\n }\n\n return ans;\n \n }\n};", "memory": "107955" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> prefix(n, 0), suffix(n, 0);\n prefix[0] = nums[0];\n suffix[n - 1] = nums[n - 1];\n for (int i = 1; i < n; i++)\n prefix[i] = prefix[i - 1] + nums[i];\n if (x > prefix[n - 1])\n return -1;\n for (int i = n - 2; i >= 0; i--)\n suffix[i] = suffix[i + 1] + nums[i];\n // 2 sum problem: suffix[i] + prefix[j] == x such that j + (n - i) is\n // minimized\n int ans = INT_MAX;\n int lb = lower_bound(prefix.begin(), prefix.end(), x) - prefix.begin();\n if (x == prefix[lb])\n ans = lb + 1;\n for (int i = n - 1; i >= 0; i--) {\n if (suffix[i] > x)\n break;\n if (suffix[i] == x) {\n ans = min(ans, n - i);\n break;\n }\n int pref_sum = x - suffix[i];\n lb = lower_bound(prefix.begin(), prefix.end(), pref_sum) - prefix.begin();\n if (lb < i && pref_sum == prefix[lb])\n ans = min(ans, lb + 1 + (n - i));\n }\n return ans == INT_MAX ? -1 : ans;\n }\n};", "memory": "107955" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size(),sum=0,mini=INT_MAX,i;\n vector<int> prfx(n);\n vector<int> sufx(n);\n for(i=0;i<n;i++){\n sum+=nums[i];prfx[i]=sum;\n }\n sum=0;\n for(i=n-1;i>=0;i--){\n sum+=nums[i];sufx[i]=sum;\n }\n for(i=0;i<n;i++){\n if(prfx[i]==x){\n mini=min(mini,i+1);\n }else if(prfx[i]>x){\n break;\n }\n int high=i+1,low=n-1,target=x-prfx[i],ind=-1;\n while(high<=low){\n int mid=(low+high)/2;\n if(target==sufx[mid]){\n ind=mid;break;\n }else if(target<sufx[mid]){\n high=mid+1;\n }else{\n low=mid-1;\n }\n }\n if(ind!=-1){\n mini=min(mini,i+1+n-ind);\n }\n }\n for(i=n-1;i>=0;i--){\n if(sufx[i]==x){\n mini=min(mini,n-i);\n }else if(sufx[i]>x){\n break;\n } \n int low=0,high=i-1,target=x-sufx[i],ind=-1;\n while(low<=high){\n int mid=(low+high)/2;\n if(target==prfx[mid]){\n ind=i;break;\n }else if(target>prfx[mid]){\n low=mid+1;\n }else{\n high=mid-1;\n }\n }\n if(ind!=-1){\n mini=min(mini,ind+1+n-i);\n }\n }\n if(mini==INT_MAX){\n return -1;\n }\n return mini;\n }\n};", "memory": "109025" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n \n \n int n=nums.size()-1;\n if(x<min(nums[0],nums[n]))return -1;\n \n vector<int>leftr(n+1),rightr(n+1);\n leftr[0]=nums[0],rightr[0]=nums[n];\n for(int i=1;i<=n;i++)leftr[i]=leftr[i-1]+nums[i];\n for(int i=n-1;i>=0;i--)rightr[n-i]=rightr[n-i-1]+nums[i];\n int ans=INT_MAX;\n int a=lower_bound(leftr.begin(),leftr.end(),x)-leftr.begin();\n int b=lower_bound(rightr.begin(),rightr.end(),x)-rightr.begin();\n if(a!=n+1)if(leftr[a]==x)ans=min(ans,a+1);\n if(b!=n+1)if(rightr[b]==x)ans=min(ans,b+1);\n \n int j=0;\n if(rightr[n]<x)return -1;\n while(j<=n)\n {\n \n if(x<leftr[j])break;\n else\n {\n int c=lower_bound(rightr.begin(),rightr.end(),x-leftr[j])-rightr.begin();\n if(c!=n+1)if(leftr[j]+rightr[c]==x)ans=min(c+1+j+1,ans);\n }\n j++;\n }\n return (ans==INT_MAX)?-1:ans;\n }\n};", "memory": "110095" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> pstart(n,0),pend(n,0);\n long long sum=0;\n for(int i=0;i<n;i++)\n {\n sum=sum+nums[i];\n pstart[i]=sum;\n }\n sum=0;\n for(int i=n-1;i>=0;i--)\n {\n sum=sum+nums[i];\n pend[i]=sum;\n }\n int ans=1e9;\n int res=1e9;\n for(int i=n-1;i>=0;i--)\n {\n int target=x-pend[i];\n if(target==0)\n {\n res=n-i;\n ans=min(ans,res);\n continue;\n }\n if(target<0)\n break;\n\n int start=0,end=i-1;\n while(start<=end)\n {\n int mid = (start+end)/2;\n if(pstart[mid]==target)\n {\n res=mid+1+(n-i);\n break;\n }\n else if(pstart[mid]>target)\n {\n end=mid-1;\n }\n else\n start=mid+1;\n }\n ans=min(ans,res);\n }\n\n\n for(int i=0;i<n;i++)\n {\n int target=x-pstart[i];\n if(target==0)\n {\n res=i+1;\n ans=min(ans,res);\n continue;\n }\n if(target<0)\n break;\n\n int start=i+1,end=n-1;\n while(start<=end)\n {\n int mid = (start+end)/2;\n if(pend[mid]==target)\n {\n res=i+1+(n-mid);\n break;\n }\n else if(pend[mid]>target)\n {\n end=mid-1;\n }\n else\n start=mid+1;\n }\n ans=min(ans,res);\n }\n return ans==1e9 ? -1 : ans;\n }\n};", "memory": "110095" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size();\n vector<int>suff;\n int sm=0;\n for(int i=n-1;i>=0;i--)\n {\n int add=0;\n if(!suff.empty())add=suff.back();\n suff.push_back(add+nums[i]);\n sm+=nums[i];\n }\n if(sm<x)return -1;\n if(sm==x)return n;\n int ans=n+2;\n int add=0;\n int idx=lower_bound(suff.begin(),suff.end(),x)-suff.begin();\n if(idx<n && suff[idx]==x)ans=idx+1;\n for(int i=0;i<n;i++)\n {\n add+=nums[i];\n if(add==x)\n {\n ans=min(ans,i+1);\n continue;\n }\n int idx=lower_bound(suff.begin(),suff.end(),x-add)-suff.begin();\n if(idx<n && suff[idx]==x-add)ans=min(ans,i+1+idx+1);\n }\n if(ans==n+2)return -1;\n return ans;\n }\n};", "memory": "111165" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=0;i<ans.size();i++)cout<<ans[i]<<\" \";\n // cout<<endl;\n while(r<ans.size()){\n \n if(chk>x){\n chk-=ans[l];\n l++;\n }else if(chk<x){\n chk+=ans[r];\n r++;\n }else{\n r--;\n // cout<<chk<<\" l \"<<l<<\" r \"<<r<<endl;\n if(l<=nums.size()-1 && r>=nums.size()-1 || l<=nums.size() && r>=nums.size() ){\n t=min(t,r-l+1);\n }\n r++;\n chk+=ans[r];\n r++;\n }\n }\n if(t==2*nums.size()+1){\n t=-1;\n }\n return t;\n }\n};", "memory": "112235" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=0;i<ans.size();i++)cout<<ans[i]<<\" \";\n // cout<<endl;\n while(r<ans.size()){\n \n if(chk>x){\n chk-=ans[l];\n l++;\n }else if(chk<x){\n chk+=ans[r];\n r++;\n }else{\n r--;\n // cout<<chk<<\" l \"<<l<<\" r \"<<r<<endl;\n if(l<=nums.size() && r>=nums.size()-1 ){\n t=min(t,r-l+1);\n }\n r++;\n chk+=ans[r];\n r++;\n }\n }\n if(t==2*nums.size()+1){\n t=-1;\n }\n return t;\n }\n};", "memory": "112235" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>ans;\n ans=nums;\n int i,j,l=0,r=0,chk=0,t=2*ans.size()+1;\n for(i=0;i<nums.size();i++){ans.push_back(nums[i]);chk+=nums[i];}\n if(chk<x)return -1;\n chk=0;\n // for(i=0;i<ans.size();i++)cout<<ans[i]<<\" \";\n // cout<<endl;\n while(r<ans.size()){\n \n if(chk>x){\n chk-=ans[l];\n l++;\n }else if(chk<x){\n chk+=ans[r];\n r++;\n }else{\n r--;\n // cout<<chk<<\" l \"<<l<<\" r \"<<r<<endl;\n if(l<=nums.size()-1 && r>=nums.size()-1 || l<=nums.size() && r>=nums.size() ){\n t=min(t,r-l+1);\n }\n r++;\n chk+=ans[r];\n r++;\n }\n }\n if(t==2*nums.size()+1){\n t=-1;\n }\n return t;\n }\n};", "memory": "113305" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unsigned int cnt = UINT_MAX;\n vector<int> lSum;\n vector<int> rSum;\n\n lSum.push_back(0);\n if (nums[0] <= x) {\n for (int i = 0; i < n; i++) {\n int s = lSum.back() + nums[i];\n if (s <= x) {\n lSum.emplace_back(s);\n } else {\n break;\n }\n }\n }\n rSum.push_back(0);\n if (nums[n-1] <= x) {\n rSum.push_back(nums[n-1]);\n for (int i = n-2; i >= 0; i--) {\n int s = rSum.back() + nums[i];\n if (s <= x) {\n rSum.emplace_back(s);\n } else {\n break;\n }\n }\n }\n // cout << \"n:\" << n << \" lSize:\" << lSum.size() << \" rSize:\" << rSum.size() << \" lLast:\" << lSum.back() << \" rLast:\" << rSum.back() << endl;\n for (int i = 0; i < lSum.size(); i++) {\n auto iter = lower_bound(rSum.begin(), rSum.end(), x-lSum[i]);\n if (iter != rSum.end() && (iter-rSum.begin())+i <= n && lSum[i]+(*iter) == x) {\n cnt = min(cnt, (unsigned int)(i+(iter-rSum.begin())));\n // cout << \" i:\" << i << \" j:\" << (iter-rSum.begin()) << \" l:\" << lSum[i] << \" r:\" << (*iter) << \" sum:\" << lSum[i]+(*iter) << \" cnt:\" << cnt << endl;\n }\n }\n\n return (int)cnt;\n }\n};", "memory": "114375" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>pref;\n int n=nums.size();\n vector<int>suff(n);\n int ans=1e9;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n pref.push_back(sum);\n if(sum==x){\n ans=min(ans,i+1);\n }\n }\n if(sum<x){\n return -1;\n }\n sum=0;\n for(int i=nums.size()-1;i>=0;i--){\n sum+=nums[i];\n suff[i]=sum;\n if(sum==x){\n ans=min(ans,n-i);\n }\n }\n \n \n int cnt=1;\n for(int i=nums.size()-1;i>=0;i--){\n int val=x-suff[i];\n int y=lower_bound(pref.begin(),pref.end(),val)-pref.begin();\n if(y<pref.size() && pref[y]==val){\n ans=min(ans,cnt+y+1);\n }\n cnt++;\n\n }\n return ans==1e9?-1:ans;\n }\n};", "memory": "115445" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n\n vector<int> nums2 ;\n for (int n: nums) {\n nums2.push_back(n);\n }\n for (int n: nums) {\n nums2.push_back(n);\n }\n int n = nums2.size();\n\n int l = 0;\n int r = 0;\n int sum = 0;\n int ans = INT_MAX;\n while (r < n) {\n sum += nums2[r];\n if (r-l+1 > n/2) {\n sum -= nums2[l];\n l++;\n if (sum == x) {\n ans = min(ans, r-l+1);\n }\n r++;\n continue;\n }\n\n if (sum == x) {\n ans = min(ans, r-l+1);\n r++;\n } else if (sum > x && (r == n/2 -1 || (r > n/2 -1 && l < n/2))) {\n while (sum > x && (r == n/2 -1 || (r > n/2 -1 && l < n/2))) {\n sum -= nums2[l];\n l++;\n }\n if (sum == x) {\n ans = min(ans, r-l+1);\n }\n r++;\n } else {\n r++;\n }\n }\n return ans == INT_MAX ? -1: ans;\n \n }\n\n //1,1,4,2,3 1 1 4 2 3\n\n};", "memory": "116515" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n vector<int>pref;\n vector<int>suff;\n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum=sum+nums[i];\n pref.push_back(sum);\n }\n sum=0;\n int n=nums.size();\n for(int i=nums.size()-1;i>=0;i--){\n sum=sum+nums[i];\n suff.push_back(sum);\n }\n int ans=1e9;\n for(int i=0;i<nums.size();i++){\n int y=x-pref[i];\n cout<<y<<\" \";\n if(y==0){\n ans=min(ans,i+1);\n continue;\n }\n if(y<0){\n break;\n }\n int index=lower_bound(suff.begin(),suff.end(),y)-suff.begin();\n cout<<index<<\" \";\n if(index<0){\n break;\n }\n if(index+i+2>=n){\n continue;\n }\n if(suff[index]==y){\n ans=min(ans,i+1+index+1);\n }\n }\n for(int i=0;i<nums.size();i++){\n int y=x-suff[i];\n if(y==0){\n ans=min(ans,i+1);\n }\n if(y<0){\n break;\n }\n int index=lower_bound(pref.begin(),pref.end(),y)-pref.begin();\n if(index<0){\n break;\n }\n if(index+i+2>=n){\n continue;\n }\n if(pref[index]==y){\n ans=min(ans,i+1+index+1);\n }\n }\n if(ans==1e9){\n return -1;\n }\n return ans;\n }\n};", "memory": "117585" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n }\n\n if (psum[n-1] < x) return -1;\n if (psum[n-1] == x) return n; \n\n for(int i = n; i > 0 and sum <= x; i--) {\n if(i<n) sum += nums[i];\n\n if(x==sum){\n minmoves = min(minmoves, n-i);\n break;\n }\n\n auto ele = lower_bound(begin(psum), begin(psum)+n, x-sum);\n n_left = ele - begin(psum);\n if(*ele == x-sum and n_left != n){\n minmoves = min(minmoves, n_left + n - i + 1);\n } \n } \n return minmoves == n ? -1 : minmoves;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "118655" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n }\n\n if (psum[n-1] < x) return -1;\n if (psum[n-1] == x) return n; \n\n for(int i = n; i > 0 and sum <= x; i--) {\n if(i<n) sum += nums[i];\n\n if(x==sum){\n minmoves = min(minmoves, n-i);\n break;\n }\n\n auto ele = lower_bound(begin(psum), begin(psum)+n, x-sum);\n n_left = ele - begin(psum);\n if(*ele == x-sum and n_left != n){\n minmoves = min(minmoves, n_left + n - i + 1);\n } \n }\n\n return minmoves == n ? -1 : minmoves;\n }\n};\n\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();", "memory": "118655" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n vector<int> psum = vector<int>(1e5);\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size(), sum = 0, n_left, minmoves = n;\n\n psum[0] = nums[0];\n\n for(int i = 1; i < n; i++) {\n psum[i] = psum[i-1] + nums[i];\n }\n\n if (psum[n-1] < x) return -1;\n if (psum[n-1] == x) return n; \n\n for(int i = n; i > 0 and sum <= x; i--) {\n if(i<n) sum += nums[i];\n\n if(x==sum){\n minmoves = min(minmoves, n-i);\n break;\n }\n\n auto ele = lower_bound(begin(psum), begin(psum)+n, x-sum);\n n_left = ele - begin(psum);\n if(*ele == x-sum and n_left != n){\n minmoves = min(minmoves, n_left + n - i + 1);\n } \n }\n\n return minmoves == n ? -1 : minmoves;\n }\n};\n\n// auto init = []() {\n// ios::sync_with_stdio(false);\n// cin.tie(0);\n// cout.tie(0);\n// return 'c';\n// }();", "memory": "119725" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int, int> prefix;\n int v = 0, retv = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n v += nums[i];\n if (v > x) break;\n if (v == x) retv = min(retv, i + 1);\n prefix[v] = i + 1;\n }\n\n v = 0;\n for (int i = nums.size() - 1; i >= 0; i--) {\n v += nums[i];\n if (v > x) break;\n if (prefix.count(x - v) && i >= prefix[x - v]) retv = min(retv, static_cast<int>(prefix[x - v] + nums.size() - i));\n if (v == x) retv = min(retv, static_cast<int>(nums.size() - i));\n }\n if (retv == INT_MAX) return -1;\n return retv;\n }\n};", "memory": "120795" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int,int> mpp;\n int n=nums.size(), sum=0;\n for(int r=n-1; r>=0; r--){\n sum+=nums[r];\n if(sum<=x)mpp[sum]=n-r;\n else break;\n if(r==0 && sum<x)return -1;\n }\n mpp[0]=0;\n int ans=((mpp.find(x)!=mpp.end())?mpp[x]:INT_MAX), total=0;\n for(int i=0; i<n; i++){\n total+=nums[i];\n if(total>x)break;\n else if(mpp.find(x-total)!=mpp.end()){\n ans=min(ans, i+1+mpp[x-total]);\n }\n }\n if(ans==INT_MAX)return -1;\n return ans;\n }\n};", "memory": "121865" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n public:\n int minOperations(vector<int>& nums, int x) {\n unordered_map<int, int> left;\n int res = INT_MAX;\n for (auto l = 0, sum = 0; l <= nums.size() && sum <= x; ++l) {\n left[sum] = l;\n if (l < nums.size())\n sum += nums[l];\n }\n for (int r = nums.size() - 1, sum = 0; r >= 0 && sum <= x; --r) {\n auto it = left.find(x - sum);\n if (it != end(left) && r + 1 >= it->second) {\n res = min(res, (int)nums.size() - r - 1 + it->second);\n }\n sum += nums[r];\n }\n return res == INT_MAX ? -1 : res;\n }\n};\n// class Solution {\n// public:\n// int minOperations(vector<int>& nums, int x) {\n// int sum = 0, n = nums.size();\n// for (int i : nums) sum += i;\n// int target = sum - x;\n// int curr_sum = 0, max_len = 0;\n// int start_idx = 0;\n// bool found = false;\n// for (int end_idx = 0; end_idx < n; end_idx++) {\n// curr_sum += nums[end_idx];\n// while (start_idx <= end_idx && curr_sum > target) {\n// curr_sum -= nums[start_idx];\n// start_idx += 1;\n// }\n// if (curr_sum == target) {\n// found = true;\n// max_len = max(max_len, end_idx - start_idx + 1);\n// }\n// }\n// return found ? n - max_len : -1;\n// }\n// };", "memory": "122935" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n // remain -> idx from left \n map<int, int> loc;\n int cur = x;\n int result = INT_MAX;\n for (int i = 0; i < nums.size(); i++) {\n cur -= nums[i];\n if (cur > 0) {\n loc[cur] = i + 1;\n } else {\n if (cur == 0) {\n result = i + 1;\n }\n break;\n }\n }\n if (cur > 0) return -1;\n cur = 0;\n for (int i = n-1; i >= 0; i--) {\n cur += nums[i];\n if (cur < x) {\n if (loc.find(cur) != loc.end()) {\n result = min((loc[cur] + n - i), result);\n }\n } else {\n if (cur == x) {\n result = min((n - i), result);\n }\n break;\n }\n }\n return result == INT_MAX ? -1 : result;\n }\n};", "memory": "124005" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n=nums.size();\n vector<int> prefixsum(n,0);\n int sum=0;\n for (int i=0;i<n;i++){\n sum+=nums[i];\n prefixsum[i]=sum;\n }\n int ans=INT_MAX;\n unordered_map<int,int> mp;\n int j=0;\n while (j<n){\n if (prefixsum[j]>x){\n break;\n }\n if (prefixsum[j]==x){\n ans=min(ans,j+1);\n break;\n }\n mp[prefixsum[j]]=j+1;\n j++;\n }\n int i=n-1;\n while (i>=0){\n if (prefixsum[n-1]-prefixsum[i]==x){\n ans=min(ans,n-i-1);\n break;\n }\n if (prefixsum[n-1]-prefixsum[i]>x){\n break;\n }\n if (mp.find(x-prefixsum[n-1]+prefixsum[i])!=mp.end()){\n ans=min(ans,n-i-1+mp[x-prefixsum[n-1]+prefixsum[i]]);\n }\n i--;\n }\n if (ans==INT_MAX || ans>nums.size()) return -1;\n return ans;\n }\n};", "memory": "125075" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "/*\n\nhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935935/Java-Detailed-Explanation-O(N)-Prefix-SumMap-Longest-Target-Sub-Array\n\nhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/936074/JavaPython-3-Sliding-window%3A-Longest-subarray-sum-to-the-target-sum(nums)-x.\n\nhttps://leetcode.com/problems/minimum-operations-to-reduce-x-to-zero/discuss/935956/C%2B%2B-O(n)-Two-Sum-and-Hash-Map\n\n*/\n\n#include <unordered_map>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n \n int n = nums.size(), total = 0, mn = INT_MAX, len = -1;\n \n for (int num : nums) {\n mn = min(mn, num);\n total += num; \n }\n \n if ( x < mn || total < x ) {\n return -1;\n }\n if ( total == x ) {\n return n;\n }\n \n/*\n int target = total - x, l = 0, r = 0, sum = 0;\n \n while (r < n) {\n \n sum += nums[r++];\n \n while ( l < r && sum > target ) {\n sum -= nums[l++];\n }\n \n if (sum == target) {\n len = max(len, r - l);\n }\n }\n*/\n int target = total - x, sum = 0;\n \n unordered_map<int, int> mp;\n mp[0] = -1;\n \n for (int i = 0; i < n; ++i) {\n \n sum += nums[i];\n \n int diff = sum - target;\n \n auto it = mp.find(diff);\n if ( it != mp.end() ) {\n len = max(len, i - it->second );\n } \n if ( mp.count(sum) == 0 ) {\n mp[sum] = i;\n }\n } \n \n return ( len == -1 ? -1 : n - len );\n }\n};", "memory": "126145" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int total_sum = 0;\n for (int num : nums) {\n total_sum += num;\n }\n \n int target = total_sum - x;\n if (target < 0) return -1;\n if (target == 0) return nums.size();\n \n unordered_map<int, int> prefix_sum_map;\n prefix_sum_map[0] = -1;\n \n int current_sum = 0;\n int max_len = -1;\n \n for (int i = 0; i < nums.size(); i++) {\n current_sum += nums[i];\n \n if (prefix_sum_map.find(current_sum - target) != prefix_sum_map.end()) {\n max_len = max(max_len, i - prefix_sum_map[current_sum - target]);\n }\n \n prefix_sum_map[current_sum] = i;\n }\n \n return max_len == -1 ? -1 : nums.size() - max_len;\n\n }\n};", "memory": "127215" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total_sum = 0;\n for (int num : nums) {\n total_sum += num;\n }\n \n int target = total_sum - x;\n if (target < 0) {\n return -1;\n }\n if (target == 0) {\n return n;\n } \n unordered_map<int, int> ps_idx;\n ps_idx[0] = -1;\n int cur_sum = 0;\n int max_len = -1;\n for (int i = 0; i < n; ++i) {\n cur_sum += nums[i];\n if (ps_idx.find(cur_sum - target) != ps_idx.end()) {\n max_len = max(max_len, i - ps_idx[cur_sum - target]);\n }\n if (ps_idx.find(cur_sum) == ps_idx.end()) {\n ps_idx[cur_sum] = i;\n }\n }\n \n if (max_len == -1) {\n return -1;\n }\n \n return n - max_len;\n\n }\n};\n", "memory": "128285" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total_sum = 0;\n\n for(int num : nums) {\n total_sum += num;\n }\n\n int target = total_sum - x;\n if(target == 0) return n; // entire array sum equals x\n if(target < 0) return -1; // x is greater than total sum\n\n unordered_map<int, int> store;\n store[0] = -1;\n int prefix_sum = 0;\n int max_len = -1;\n\n for(int i = 0; i < n; i++) {\n prefix_sum += nums[i];\n\n if(store.find(prefix_sum - target) != store.end()) {\n max_len = max(max_len, i - store[prefix_sum - target]);\n }\n\n store[prefix_sum] = i;\n }\n\n return max_len == -1 ? -1 : n - max_len;\n }\n};\n", "memory": "129355" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unordered_map<int,int> m;\n m[0] = -1;\n int sum = 0;\n for(auto a : nums)\n sum+=a;\n if(sum == x) return n;\n if(sum < x) return -1;\n int target = sum-x;\n int s = 0 , ans = 0;\n for(int i = 0 ; i < n ; ++i){\n s += nums[i];\n if(m.count(s-target))\n ans = max(ans, i-m[s-target]);\n m[s] = i;\n }\n return ans > 0 ? n - ans : -1;\n }\n};", "memory": "130425" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\n\n int helper (vector <int> &nums, int &target){\n int n = nums.size();\n unordered_map <int, int> mp;\n mp[0] = -1;\n int sum = 0, maxlen = 0;\n for (int i = 0; i < n; i++){\n sum += nums[i];\n\n mp[sum] = i;\n\n if (mp.find(sum-target) != mp.end()){\n maxlen = max (maxlen, i-mp[sum-target]);\n }\n }\n return maxlen;\n }\npublic:\n int minOperations(vector<int>& nums, int x) {\n int sum = 0;\n for (int num : nums) sum += num;\n int n = nums.size();\n int target = sum - x;\n if (x == sum) return n;\n int ans = helper (nums, target); \n if (ans == 0) return -1;\n return ans == 0 ? -1 : n-ans;\n }\n};", "memory": "131495" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n const int sum = accumulate(nums.begin(), nums.end(), 0); \n const int target = sum - x;\n if (target == 0) {\n return nums.size();\n }\n // find the longest subarray whose sum is target.\n const int len = LongestSubarray(nums, target);\n if (len == 0) {\n return -1;\n }\n return nums.size() - len;\n }\nprivate:\n int LongestSubarray(const vector<int>& nums, int target) {\n unordered_map<int, int> m; // prefix sum v.s. first index.\n int sum = 0;\n m[0] = -1;\n int ans = 0;\n\n for (int i = 0; i < nums.size(); ++ i) {\n sum += nums[i];\n const auto it = m.find(sum - target);\n if (it != m.end()) {\n ans = max(ans, i - it->second);\n }\n if (m.count(sum) == 0) {\n m[sum] = i;\n }\n }\n\n return ans;\n }\n};", "memory": "132565" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int goal) {\n //largest subarray whose sum is x\n int n = nums.size();\n int presum=0;\n int larg=0;\n int x=0;\n for(auto it:nums)\n x+=it;\n x-=goal;\n if(x==0)\n return n;\n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0;i<n;i++)\n {\n presum+=nums[i];\n if(mp.find(presum-x)!=mp.end())\n larg=max(larg,i-mp[presum-x]);\n if(mp.find(presum)==mp.end())\n mp[presum]=i;\n }\n if(larg==0)\n return -1;\n return n-larg;\n }\n};", "memory": "133635" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n;\n \n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0;i<n;i++){\n if(i!=0){\n preSum[i] +=preSum[i-1];\n }\n int temp = preSum[i]-target;\n if(mp.find(temp) != mp.end()){\n ans = min(ans,n-i+mp[temp]);\n }\n cout<<endl;\n if(mp.find(preSum[i]) == mp.end()){\n mp[preSum[i]] = i;\n }\n \n }\n return ans!=INT_MAX ? ans: -1;\n }\n}; ", "memory": "141125" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n;\n \n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0;i<n;i++){\n if(i!=0){\n preSum[i] +=preSum[i-1];\n }\n int temp = preSum[i]-target;\n if(mp.find(temp) != mp.end()){\n ans = min(ans,n-i+mp[temp]);\n }\n cout<<endl;\n if(mp.find(preSum[i]) == mp.end()){\n mp[preSum[i]] = i;\n }\n \n }\n return ans!=INT_MAX ? ans: -1;\n }\n}; ", "memory": "142195" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n vector<int> preSum=nums;\n int ans=INT_MAX;\n int target= -x;\n \n for(int i=0;i<n;i++){\n target += nums[i];\n }\n if(target==0) return n;\n \n unordered_map<int,int> mp;\n mp[0]=-1;\n for(int i=0;i<n;i++){\n if(i!=0){\n preSum[i] +=preSum[i-1];\n }\n cout<<preSum[i]<< \" \";\n int temp = preSum[i]-target;\n if(mp.find(temp) != mp.end()){\n cout<<ans<< \" \";\n ans = min(ans,n-i+mp[temp]);\n }\n cout<<endl;\n if(mp.find(preSum[i]) == mp.end()){\n mp[preSum[i]] = i;\n }\n \n }\n return ans!=INT_MAX ? ans: -1;\n }\n}; ", "memory": "143265" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int tot = accumulate(nums.begin(),nums.end(),0);\n int n = nums.size();\n int target = tot - x;\n if(target == 0)return n;\n map<int,int> mp;\n mp[0] = -1;\n int maxi = 0;\n int sum =0;\n for(int i=0;i<n;i++){\n sum += nums[i];\n if(mp.find(sum-target)!=mp.end()){\n maxi = max(maxi, i - mp[sum-target]);\n }\n if(mp.find(sum)==mp.end()){\n mp[sum] = i;\n }\n }\n if(maxi == 0)return -1;\n return n - maxi;\n }\n};", "memory": "144335" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& a, int x) {\n long long s = 0;\n for(auto i:a)\n s += i;\n \n s = s - x;\n if(s==0)\n return (int)a.size();\n \n map<long long, int> d;\n\n d[0] = -1;\n int t = 0;\n int ans = 0;\n\n for(int i=0;i<(int)a.size();i++){\n t += a[i];\n if(d.count(t - s))\n ans = max(ans, i - d[t-s]);\n if(d.count(t)==0)\n d[t] = i;\n }\n if(ans==0)\n return -1;\n\n return (int)a.size() - ans;\n \n }\n};", "memory": "145405" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int sum = 0;\n for(auto n:nums){\n sum += n;\n }\n int find = sum - x;\n if(find == 0){\n return nums.size();\n }\n map<int,int> m;\n m[0] = -1;\n int add = 0;\n int N = nums.size();\n int ret = INT_MAX;\n for(int i = 0;i < nums.size(); ++i){\n add += nums[i];\n if(m.count(add - find)){\n ret = min(ret, N-i + m[add-find]);\n }\n m[add] = i;\n }\n if(ret == INT_MAX) return -1;\n return ret;\n }\n};", "memory": "146475" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int len = INT_MIN;\n int s = 0;\n for(int i = 0; i < n; i++){\n s += nums[i];\n }\n if(s == x){\n return n;\n }\n int sum = s-x;\n int currSum = 0;\n \n map<int,int> mp;\n mp[0] = -1;\n for(int i = 0; i < n; i++){\n currSum += nums[i];\n if(mp.find(currSum-sum) != mp.end()){\n len = max(len, i - mp[currSum-sum]);\n }\n \n mp[currSum] = i;\n \n }\n if(len == INT_MIN){\n return -1;\n }\n return n-len;\n }\n};", "memory": "147545" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int total = accumulate(nums.begin(),nums.end(),0);\n int target = total - x;\n if(target < 0) return -1;\n if(target == 0) return n;\n int maxLen = -1;\n unordered_map<long long,int> mp;\n long long sum = 0;\n for(int i=0;i<n;i++){\n sum += nums[i];\n if(sum == target) maxLen = max(maxLen,i+1);\n long long rem = sum - target;\n if(mp.find(rem) != mp.end()){\n maxLen = max(maxLen,i-mp[rem]);\n }\n if(mp.find(sum) == mp.end()) mp[sum] = i;\n }\n return maxLen != -1 ? n-maxLen :-1;\n }\n};", "memory": "148615" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int res = n + 1;\n int t_sum = 0;\n for (int i : nums) {\n t_sum += i;\n }\n int k = t_sum - x;\n if (k < 0) return -1;\n unordered_map<int, int> ps_ar;\n int pre_sum = 0;\n ps_ar[0] = -1;\n \n for (int i = 0; i < n; i++) {\n pre_sum += nums[i];\n if(pre_sum==x) res=min(res,i+1);\n ps_ar[pre_sum] = i;\n }\n \n int suf_sum = 0;\n \n \n for (int j = n - 1; j >= 0; j--) {\n suf_sum += nums[j];\n if(suf_sum == x) res=min(res,n-j);\n int req = x - suf_sum;\n \n if (ps_ar.find(req) != ps_ar.end()) {\n int idx = ps_ar[req];\n res = min(res, (n - j) + (idx + 1));\n }\n }\n \n return res == n + 1 ? -1 : res;\n }\n};\n", "memory": "149685" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int totalSum = accumulate(nums.begin(), nums.end(), 0);\n int target = totalSum - x;\n \n if (target < 0) return -1;\n \n unordered_map<int, int> prefixSum;\n prefixSum[0] = -1;\n \n int currentSum = 0;\n int maxLength = -1;\n \n for (int i = 0; i < nums.size(); i++) {\n currentSum += nums[i];\n \n if (prefixSum.find(currentSum - target) != prefixSum.end()) {\n maxLength = max(maxLength, i - prefixSum[currentSum - target]);\n }\n \n if (prefixSum.find(currentSum) == prefixSum.end()) {\n prefixSum[currentSum] = i;\n }\n }\n if(target==0 && maxLength==-1){return nums.size();}\n return maxLength == -1 ? -1 : nums.size() - maxLength;\n }\n};\n", "memory": "150755" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxSubArrayLen(vector<int>& nums, int k) {\n\n //[1,-1,5,-2,3]. k=3\n //[1,0, 5, 3,6]\n\n // -2,-1,2,1 k=1\n // -2, -3,-1,0\n // -2 -4, -2,-1\n unordered_map<long long, int>mmap;\n mmap[0]=-1;\n int res=0;\n long long sum = 0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n if(mmap.find(sum-k) != mmap.end()){\n res= max(res, i-mmap[sum-k]);\n }\n if(mmap.find(sum) == mmap.end()){\n mmap[sum]=i;\n }\n }\n return res;\n }\n\n int minOperations(vector<int>& nums, int x) {\n \n int sum=0;\n for(int i=0;i<nums.size();i++){\n sum+=nums[i];\n }\n int rem = sum -x; //required max len subarray with sum ==rem\n if(rem==0){\n return nums.size();\n }\n int length = maxSubArrayLen(nums, rem);\n if(length==0) return -1;\n \n return nums.size()-length;\n }\n};", "memory": "151825" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n unordered_map<int, int> hmap;\n int minOps = INT_MAX;\n int curSum = 0;\n\n for(int i=0; i<n; ++i) {\n curSum += nums[i];\n if(curSum == x) {\n minOps = min(minOps, i+1);\n }\n\n hmap[curSum] = i;\n }\n\n curSum = 0;\n for(int i = n-1; i>=0; --i) {\n curSum += nums[i];\n if(curSum == x) {\n minOps = min(minOps, n-i);\n }\n\n if(hmap.find(x - curSum) != hmap.end() && hmap[x-curSum] < i) {\n int opsLeft = hmap[x-curSum] + 1;\n int opsRight = n-i;\n minOps = min(minOps, opsLeft + opsRight);\n }\n }\n\n return minOps == INT_MAX ? -1 : minOps;\n }\n};", "memory": "152895" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n x = accumulate(nums.begin(), nums.end(), 0) - x;\n unordered_map<int, int> vis{ {0, -1} };\n int n = nums.size();\n int ans = 1 << 30;\n for (int i = 0, s = 0; i < n; ++i) {\n s += nums[i];\n if (!vis.count(s)) {\n vis[s] = i;\n }\n if (vis.count(s - x)) {\n int j = vis[s - x];\n ans = min(ans, n - (i - j));\n }\n }\n return ans == 1 << 30 ? -1 : ans;\n }\n};", "memory": "153965" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <unordered_map>\n#include <algorithm>\n\nclass Solution {\npublic:\n int minOperations(vector<int>& nums, int x) {\n int n = nums.size();\n int res = n + 1;\n int t_sum = 0;\n unordered_map<int, int> ps_ar;\n int pre_sum = 0;\n ps_ar[0] = -1;\n \n for (int i = 0; i < n; i++) {\n pre_sum += nums[i];\n if(pre_sum==x) res=min(res,i+1);\n ps_ar[pre_sum] = i;\n }\n \n int suf_sum = 0;\n \n \n for (int j = n - 1; j >= 0; j--) {\n suf_sum += nums[j];\n if(suf_sum == x) res=min(res,n-j);\n int req = x - suf_sum;\n \n if (ps_ar.find(req) != ps_ar.end()) {\n int idx = ps_ar[req];\n res = min(res, (n - j) + (idx + 1));\n }\n }\n \n return res == n + 1 ? -1 : res;\n }\n};\n", "memory": "155035" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n #define ll long long\n int minOperations(vector<int>& nums, int x) {\n // longest subarray with sum = total-x\n int n = nums.size();\n ll totalSum = 0;\n for(int num: nums) totalSum += num;\n int maxLength = 0;\n ll target = totalSum-x;\n unordered_map<int, int> lastSeen;\n lastSeen[0] = -1;\n ll currSum = 0;\n for(int i=0; i<n; i++){\n currSum+= nums[i];\n if(lastSeen.find(currSum-target) != lastSeen.end()){\n maxLength = max(maxLength, i-lastSeen[currSum-target]);\n }\n if(lastSeen.find(currSum) == lastSeen.end()){\n lastSeen[currSum] = i;\n }\n }\n if(maxLength > 0) return n-maxLength;\n if(target == 0) return n;\n return -1;\n }\n};", "memory": "156105" }
1,776
<p>You are given an integer array <code>nums</code> and an integer <code>x</code>. In one operation, you can either remove the leftmost or the rightmost element from the array <code>nums</code> and subtract its value from <code>x</code>. Note that this <strong>modifies</strong> the array for future operations.</p> <p>Return <em>the <strong>minimum number</strong> of operations to reduce </em><code>x</code> <em>to <strong>exactly</strong></em> <code>0</code> <em>if it is possible</em><em>, otherwise, return </em><code>-1</code>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,1,4,2,3], x = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> The optimal solution is to remove the last two elements to reduce x to zero. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [5,6,7,8,9], x = 4 <strong>Output:</strong> -1 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,2,20,1,1,3], x = 10 <strong>Output:</strong> 5 <strong>Explanation:</strong> The optimal solution is to remove the last three elements and the first two elements (5 operations in total) to reduce x to zero. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li> <li><code>1 &lt;= x &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int minOperations(vector<int>& nums, int x) { \n int n = nums.size();\n int sum = 0;\n unordered_map<int, int> mp;\n mp[0] = -1;\n for(int i = 0; i<n; i++) {\n sum += nums[i];\n mp[sum] = i;\n }\n if(sum < x)\n return -1;\n int restSum = sum-x;\n int longest = INT_MIN;\n sum = 0;\n for(int i = 0; i<n; i++) {\n sum += nums[i];\n if(mp.count(sum-restSum)) {\n longest = max(longest, i-mp[sum-restSum]);\n }\n }\n \n return longest==INT_MIN?-1:n-longest;\n }\n};", "memory": "157175" }