{"id":6689,"date":"2020-05-03T19:44:11","date_gmt":"2020-05-04T02:44:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6689"},"modified":"2020-05-03T19:44:12","modified_gmt":"2020-05-04T02:44:12","slug":"leetcode-1433-check-if-a-string-can-break-another-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1433-check-if-a-string-can-break-another-string\/","title":{"rendered":"LeetCode 1433. Check If a String Can Break Another String"},"content":{"rendered":"\n<p>Given two strings:&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;with the same&nbsp;size, check if some&nbsp;permutation of string&nbsp;<code>s1<\/code>&nbsp;can break&nbsp;some&nbsp;permutation of string&nbsp;<code>s2<\/code>&nbsp;or vice-versa (in other words&nbsp;<code>s2<\/code>&nbsp;can break&nbsp;<code>s1<\/code>).<\/p>\n\n\n\n<p>A string&nbsp;<code>x<\/code>&nbsp;can break&nbsp;string&nbsp;<code>y<\/code>&nbsp;(both of size&nbsp;<code>n<\/code>) if&nbsp;<code>x[i] &gt;= y[i]<\/code>&nbsp;(in alphabetical order)&nbsp;for all&nbsp;<code>i<\/code>&nbsp;between&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>n-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"abc\", s2 = \"xya\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> \"ayx\" is a permutation of s2=\"xya\" which can break to string \"abc\" which is a permutation of s1=\"abc\".\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"abe\", s2 = \"acd\"\n<strong>Output:<\/strong> false \n<strong>Explanation:<\/strong> All permutations for s1=\"abe\" are: \"abe\", \"aeb\", \"bae\", \"bea\", \"eab\" and \"eba\" and all permutation for s2=\"acd\" are: \"acd\", \"adc\", \"cad\", \"cda\", \"dac\" and \"dca\". However, there is not any permutation from s1 which can break some permutation from s2 and vice-versa.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"leetcodee\", s2 = \"interview\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s1.length == n<\/code><\/li><li><code>s2.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10^5<\/code><\/li><li>All strings consist of lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Sort and compare<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkIfCanBreak(string s1, string s2) {\n    sort(begin(s1), end(s1));\n    sort(begin(s2), end(s2));\n    \n    int f1 = 1;\n    int f2 = 1;\n    for (int i = 0; i < s1.size(); ++i) {\n      f1 &#038;= s1[i] <= s2[i];\n      f2 &#038;= s1[i] >= s2[i];\n    }\n    \n    return f1 || f2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Counting Sort<\/strong><\/h2>\n\n\n\n<p>The cumulative number of elements must be monotonic e.g. t1 >= t2 or t2 >= t1 for all the letters.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool checkIfCanBreak(string s1, string s2) {\n    vector<int> c1(26);\n    vector<int> c2(26);\n    for (int i = 0; i < s1.length(); ++i) {\n      ++c1[s1[i] - 'a'];\n      ++c2[s2[i] - 'a'];\n    }\n    \n    int t1 = 0;\n    int t2 = 0;\n    int f1 = 1;\n    int f2 = 1;\n    for (int i = 0; i < 26; ++i) {\n      t1 += c1[i];\n      t2 += c2[i];\n      f1 &#038;= t1 <= t2;\n      f2 &#038;= t2 <= t1;\n    }\n    \n    return f1 || f2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two strings:&nbsp;s1&nbsp;and&nbsp;s2&nbsp;with the same&nbsp;size, check if some&nbsp;permutation of string&nbsp;s1&nbsp;can break&nbsp;some&nbsp;permutation of string&nbsp;s2&nbsp;or vice-versa (in other words&nbsp;s2&nbsp;can break&nbsp;s1). A string&nbsp;x&nbsp;can break&nbsp;string&nbsp;y&nbsp;(both of size&nbsp;n) if&nbsp;x[i] &gt;=&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[],"class_list":["post-6689","post","type-post","status-publish","format-standard","hentry","category-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6689","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6689"}],"version-history":[{"count":1,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6689\/revisions"}],"predecessor-version":[{"id":6690,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6689\/revisions\/6690"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6689"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6689"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6689"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}