Skip to content

Commit a3696ff

Browse files
committed
1915-CHECK-IF-ONE-STRING-SWAP-CAN-MAKE-STRINGS-EQUAL Stats: Time: 0 ms (100%), Space: 8.3 MB (61.87%) - LeetHub
1 parent 48452fe commit a3696ff

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
bool areAlmostEqual(string s1, string s2) {
4+
if(s1 == s2) return true;
5+
int cnt =0;
6+
for(int i=0;i<s1.size();i++){
7+
if(s1[i] != s2[i]){
8+
cnt++;
9+
}
10+
}
11+
sort(s1.begin(),s1.end());
12+
sort(s2.begin(),s2.end());
13+
if(cnt == 2 && s1 == s2){
14+
return true;
15+
}
16+
return false;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<h2><a href="https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal">1915. Check if One String Swap Can Make Strings Equal</a></h2><h3>Easy</h3><hr><p>You are given two strings <code>s1</code> and <code>s2</code> of equal length. A <strong>string swap</strong> is an operation where you choose two indices in a string (not necessarily different) and swap the characters at these indices.</p>
2+
3+
<p>Return <code>true</code> <em>if it is possible to make both strings equal by performing <strong>at most one string swap </strong>on <strong>exactly one</strong> of the strings. </em>Otherwise, return <code>false</code>.</p>
4+
5+
<p>&nbsp;</p>
6+
<p><strong class="example">Example 1:</strong></p>
7+
8+
<pre>
9+
<strong>Input:</strong> s1 = &quot;bank&quot;, s2 = &quot;kanb&quot;
10+
<strong>Output:</strong> true
11+
<strong>Explanation:</strong> For example, swap the first character with the last character of s2 to make &quot;bank&quot;.
12+
</pre>
13+
14+
<p><strong class="example">Example 2:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong> s1 = &quot;attack&quot;, s2 = &quot;defend&quot;
18+
<strong>Output:</strong> false
19+
<strong>Explanation:</strong> It is impossible to make them equal with one string swap.
20+
</pre>
21+
22+
<p><strong class="example">Example 3:</strong></p>
23+
24+
<pre>
25+
<strong>Input:</strong> s1 = &quot;kelb&quot;, s2 = &quot;kelb&quot;
26+
<strong>Output:</strong> true
27+
<strong>Explanation:</strong> The two strings are already equal, so no string swap operation is required.
28+
</pre>
29+
30+
<p>&nbsp;</p>
31+
<p><strong>Constraints:</strong></p>
32+
33+
<ul>
34+
<li><code>1 &lt;= s1.length, s2.length &lt;= 100</code></li>
35+
<li><code>s1.length == s2.length</code></li>
36+
<li><code>s1</code> and <code>s2</code> consist of only lowercase English letters.</li>
37+
</ul>

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ this is readme. here i will make
3838
| [1502-construct-k-palindrome-strings](https://github.com/TheVinaySagar/Leetcode/tree/master/1502-construct-k-palindrome-strings) |
3939
| [1524-string-matching-in-an-array](https://github.com/TheVinaySagar/Leetcode/tree/master/1524-string-matching-in-an-array) |
4040
| [1895-minimum-number-of-operations-to-move-all-balls-to-each-box](https://github.com/TheVinaySagar/Leetcode/tree/master/1895-minimum-number-of-operations-to-move-all-balls-to-each-box) |
41+
| [1915-check-if-one-string-swap-can-make-strings-equal](https://github.com/TheVinaySagar/Leetcode/tree/master/1915-check-if-one-string-swap-can-make-strings-equal) |
4142
| [2221-check-if-a-parentheses-string-can-be-valid](https://github.com/TheVinaySagar/Leetcode/tree/master/2221-check-if-a-parentheses-string-can-be-valid) |
4243
| [2292-counting-words-with-a-given-prefix](https://github.com/TheVinaySagar/Leetcode/tree/master/2292-counting-words-with-a-given-prefix) |
4344
| [2465-shifting-letters-ii](https://github.com/TheVinaySagar/Leetcode/tree/master/2465-shifting-letters-ii) |
@@ -63,6 +64,7 @@ this is readme. here i will make
6364
| [0952-word-subsets](https://github.com/TheVinaySagar/Leetcode/tree/master/0952-word-subsets) |
6465
| [1029-vertical-order-traversal-of-a-binary-tree](https://github.com/TheVinaySagar/Leetcode/tree/master/1029-vertical-order-traversal-of-a-binary-tree) |
6566
| [1502-construct-k-palindrome-strings](https://github.com/TheVinaySagar/Leetcode/tree/master/1502-construct-k-palindrome-strings) |
67+
| [1915-check-if-one-string-swap-can-make-strings-equal](https://github.com/TheVinaySagar/Leetcode/tree/master/1915-check-if-one-string-swap-can-make-strings-equal) |
6668
| [2685-first-completely-painted-row-or-column](https://github.com/TheVinaySagar/Leetcode/tree/master/2685-first-completely-painted-row-or-column) |
6769
| [2766-find-the-prefix-common-array-of-two-arrays](https://github.com/TheVinaySagar/Leetcode/tree/master/2766-find-the-prefix-common-array-of-two-arrays) |
6870
| [3455-minimum-length-of-string-after-operations](https://github.com/TheVinaySagar/Leetcode/tree/master/3455-minimum-length-of-string-after-operations) |
@@ -178,6 +180,7 @@ this is readme. here i will make
178180
| ------- |
179181
| [1396-count-servers-that-communicate](https://github.com/TheVinaySagar/Leetcode/tree/master/1396-count-servers-that-communicate) |
180182
| [1502-construct-k-palindrome-strings](https://github.com/TheVinaySagar/Leetcode/tree/master/1502-construct-k-palindrome-strings) |
183+
| [1915-check-if-one-string-swap-can-make-strings-equal](https://github.com/TheVinaySagar/Leetcode/tree/master/1915-check-if-one-string-swap-can-make-strings-equal) |
181184
| [3455-minimum-length-of-string-after-operations](https://github.com/TheVinaySagar/Leetcode/tree/master/3455-minimum-length-of-string-after-operations) |
182185
## Stack
183186
| |

stats.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"leetcode":{"shas":{"README.md":{"":"e4b17c98059d06477e09dddbe1bf0037a1c8cbc6"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"2c88a80dfe972059ae86c77fdc3f00c62982cd4a","difficulty":"medium"},"1878-check-if-array-is-sorted-and-rotated":{"sha":"cc065e516e58d8498b50ea24f3b1a51e211b6010","difficulty":"easy"},"0733-flood-fill":{"sha":"88c20887f3a61a64055ccee8d80b855da3be9494","difficulty":"easy"},"3372-longest-strictly-increasing-or-strictly-decreasing-subarray":{"sha":"1cd3b8875128b889ce353aed4f71a4ef22a96804","difficulty":"easy"},"1927-maximum-ascending-subarray-sum":{"sha":"","difficulty":"easy"}},"solved":43,"easy":12,"medium":24,"hard":7}}
1+
{"leetcode":{"shas":{"README.md":{"":"b078747e6d4df48663c3c93cd22d6d104b5fd218"},"2465-shifting-letters-ii":{"sha":"d185b9a482db6f49393149aabed8a625bb6c4d4b","difficulty":"medium"},"1895-minimum-number-of-operations-to-move-all-balls-to-each-box":{"sha":"8cf49a54282d1cc0785f31978f229b45fe17ed37","difficulty":"medium"},"1524-string-matching-in-an-array":{"sha":"1e81100d7eb5724cd55903ee2d9d62124949a6a4","difficulty":"easy"},"1029-vertical-order-traversal-of-a-binary-tree":{"sha":"233be19d0cea32f5988fad6d651954cba5c9ed45","difficulty":"hard"},"0199-binary-tree-right-side-view":{"sha":"3217ef07997f85fa8d961bc4b52603ea40379e9b","difficulty":"medium"},"3309-count-prefix-and-suffix-pairs-i":{"sha":"6f1dcc761a6930bc80b7079393535bee8875e8ed","difficulty":"easy"},"0101-symmetric-tree":{"sha":"71468a287e3dc86a1a7d1e30b92c1d32f4b8fb82","difficulty":"easy"},"0121-best-time-to-buy-and-sell-stock":{"sha":"28885475707cf6030f96ff8c6b4f34f70cae2bc8","difficulty":"easy"},"2292-counting-words-with-a-given-prefix":{"sha":"e212e615d1226f7aa1fe914321a6de10cad22ec2","difficulty":"easy"},"0236-lowest-common-ancestor-of-a-binary-tree":{"sha":"0c991325621d187084183637a371159a21446339","difficulty":"medium"},"0662-maximum-width-of-binary-tree":{"sha":"110c4ddfaa298c100c10bc2fe492f28c05cc2ee7","difficulty":"medium"},"0952-word-subsets":{"sha":"a29373661158716bb8b0ccf00e3158ba370fc293","difficulty":"medium"},"0893-all-nodes-distance-k-in-binary-tree":{"sha":"258c574ced99a128a788c3eaa0cd159c20ab3e93","difficulty":"medium"},"0222-count-complete-tree-nodes":{"sha":"6c1ff1fde6115833071a176f1ad405866db12436","difficulty":"easy"},"1502-construct-k-palindrome-strings":{"sha":"f54a64510560bfd162fa61d6793cae9cc16b372d","difficulty":"medium"},"2221-check-if-a-parentheses-string-can-be-valid":{"sha":"86ca2c40bfac3acc2e4c98f7f7620cef96e24717","difficulty":"medium"},"3455-minimum-length-of-string-after-operations":{"sha":"3d63cb0e94ee6ee5d8bde444db227e80b397a703","difficulty":"medium"},"2766-find-the-prefix-common-array-of-two-arrays":{"sha":"1f536c0b4f2a0bddd73ebf3c8a02372411662889","difficulty":"medium"},"2509-minimize-xor":{"sha":"859be97d5d5eb05646d28e37d1f57f9bee76bc38","difficulty":"medium"},"0106-construct-binary-tree-from-inorder-and-postorder-traversal":{"sha":"b251746ba3488013aaa6db4afea2f6175f0f1a56","difficulty":"medium"},"0297-serialize-and-deserialize-binary-tree":{"sha":"f31632db19b9ca20446158b4b73b5260eff612a0","difficulty":"hard"},"2792-neighboring-bitwise-xor":{"sha":"0c7f84b7ab7903d479efae54727a290528e52e3a","difficulty":"medium"},"1485-minimum-cost-to-make-at-least-one-valid-path-in-a-grid":{"sha":"7871ed4476bc954d2e44ce5efb8940b5040345ab","difficulty":"hard"},"0407-trapping-rain-water-ii":{"sha":"435cb721c56f2db44c6b235bc5304182db10371b","difficulty":"hard"},"2685-first-completely-painted-row-or-column":{"sha":"003bfba52059b42334a974f8926c961dde330c83","difficulty":"medium"},"2145-grid-game":{"sha":"10ceaba68f9e680b9c110702bce7f10038991b28","difficulty":"medium"},"1876-map-of-highest-peak":{"sha":"4ae04ec1ad12335b5ad550fd6249cd57c8610633","difficulty":"medium"},"1396-count-servers-that-communicate":{"sha":"92c1e705b7495e8ee7e1fd6027d0b9ba2aa8122f","difficulty":"medium"},"0783-search-in-a-binary-search-tree":{"sha":"d2ae96c0c9681043ea392f7b81b58ec115bdd113","difficulty":"easy"},"0784-insert-into-a-binary-search-tree":{"sha":"","difficulty":"medium"},"1558-course-schedule-iv":{"sha":"badda8021673e3e997225736414be21623410e12","difficulty":"medium"},"3439-find-minimum-diameter-after-merging-two-trees":{"sha":"eaf85fa397a1b5b21272a1c7a4dfc2a19cf30304","difficulty":"hard"},"2764-maximum-number-of-fish-in-a-grid":{"sha":"d00f05e94cc685b98a97cbb577435e2cc15e2092","difficulty":"medium"},"0684-redundant-connection":{"sha":"880ddaa2e6c6d7fd457efdfd3e5a1c962ab9b667","difficulty":"medium"},"2583-divide-nodes-into-the-maximum-number-of-groups":{"sha":"24c405e0bdc8200a64b90001e9dc5f4fb5ee5f44","difficulty":"hard"},"1036-rotting-oranges":{"sha":"0d30a8ed29926c3799eef6d17c065d0b7e94b902","difficulty":"medium"},"0854-making-a-large-island":{"sha":"67c0ad3a35f375cb21edb0fb78cacf5a278ec244","difficulty":"hard"},"3429-special-array-i":{"sha":"ef6cee63d0af1b6f4b4171c2297be6337909168b","difficulty":"easy"},"0547-number-of-provinces":{"sha":"2c88a80dfe972059ae86c77fdc3f00c62982cd4a","difficulty":"medium"},"1878-check-if-array-is-sorted-and-rotated":{"sha":"cc065e516e58d8498b50ea24f3b1a51e211b6010","difficulty":"easy"},"0733-flood-fill":{"sha":"88c20887f3a61a64055ccee8d80b855da3be9494","difficulty":"easy"},"3372-longest-strictly-increasing-or-strictly-decreasing-subarray":{"sha":"1cd3b8875128b889ce353aed4f71a4ef22a96804","difficulty":"easy"},"1927-maximum-ascending-subarray-sum":{"sha":"48452fe63b61e5477fbb1f68b5bbd356224d5932","difficulty":"easy"},"1915-check-if-one-string-swap-can-make-strings-equal":{"sha":"","difficulty":"easy"}},"solved":44,"easy":13,"medium":24,"hard":7}}

0 commit comments

Comments
 (0)