{"id":8231,"date":"2021-03-13T21:48:25","date_gmt":"2021-03-14T05:48:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8231"},"modified":"2021-03-13T21:48:40","modified_gmt":"2021-03-14T05:48:40","slug":"leetcode-1790-check-if-one-string-swap-can-make-strings-equal","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1790-check-if-one-string-swap-can-make-strings-equal\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1790. Check if One String Swap Can Make Strings Equal"},"content":{"rendered":"\n<p>You are given two strings&nbsp;<code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;of equal length. A&nbsp;<strong>string swap<\/strong>&nbsp;is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code>&nbsp;<em>if it is possible to make both strings equal by performing&nbsp;<strong>at most one string swap&nbsp;<\/strong>on&nbsp;<strong>exactly one<\/strong>&nbsp;of the strings.&nbsp;<\/em>Otherwise, return&nbsp;<code>false<\/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 = \"bank\", s2 = \"kanb\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> For example, swap the first character with the last character of s2 to make \"bank\".\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 = \"attack\", s2 = \"defend\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> It is impossible to make them equal with one string swap.\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 = \"kelb\", s2 = \"kelb\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The two strings are already equal, so no string swap operation is required.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s1 = \"abcd\", s2 = \"dcba\"\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s1.length, s2.length &lt;= 100<\/code><\/li><li><code>s1.length == s2.length<\/code><\/li><li><code>s1<\/code>&nbsp;and&nbsp;<code>s2<\/code>&nbsp;consist of only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Remember two indices<\/strong><\/h2>\n\n\n\n<p>There needs to be either 0 or 2 indices are different. Otherwise return false.<br>s1[idx1] == s2[idx2] and s1[idx2] == s2[idx1]<\/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 areAlmostEqual(string s1, string s2) {\n    vector<int> idx;\n    for (int i = 0; i < s1.length() &#038;&#038; idx.size() <= 2; ++i)\n      if (s1[i] != s2[i]) idx.push_back(i);\n    if (idx.size() == 0) return true;\n    if (idx.size() != 2) return false;\n    return s1[idx[0]] == s2[idx[1]] &#038;&#038; s1[idx[1]] == s2[idx[0]];\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two strings&nbsp;s1&nbsp;and&nbsp;s2&nbsp;of equal length. A&nbsp;string swap&nbsp;is an operation where you choose two indices in a string (not necessarily different) and swap the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,4],"class_list":["post-8231","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8231","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=8231"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8231\/revisions"}],"predecessor-version":[{"id":8233,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8231\/revisions\/8233"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}