Skip to content

Commit b371c8b

Browse files
committed
Add solution #1790
1 parent 14a4685 commit b371c8b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@
462462
1765|[Map of Highest Peak](./1765-map-of-highest-peak.js)|Medium|
463463
1768|[Merge Strings Alternately](./1768-merge-strings-alternately.js)|Easy|
464464
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
465+
1790|[Check if One String Swap Can Make Strings Equal](./1790-check-if-one-string-swap-can-make-strings-equal.js)|Easy|
465466
1791|[Find Center of Star Graph](./1791-find-center-of-star-graph.js)|Easy|
466467
1812|[Determine Color of a Chessboard Square](./1812-determine-color-of-a-chessboard-square.js)|Easy|
467468
1817|[Finding the Users Active Minutes](./1817-finding-the-users-active-minutes.js)|Medium|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* 1790. Check if One String Swap Can Make Strings Equal
3+
* https://leetcode.com/problems/check-if-one-string-swap-can-make-strings-equal/
4+
* Difficulty: Easy
5+
*
6+
* You are given two strings s1 and s2 of equal length. A string swap is an operation where
7+
* you choose two indices in a string (not necessarily different) and swap the characters
8+
* at these indices.
9+
*
10+
* Return true if it is possible to make both strings equal by performing at most one string
11+
* swap on exactly one of the strings. Otherwise, return false.
12+
*/
13+
14+
/**
15+
* @param {string} s1
16+
* @param {string} s2
17+
* @return {boolean}
18+
*/
19+
var areAlmostEqual = function(s1, s2) {
20+
const indices = [];
21+
for (const i in s1) {
22+
if (s1[i] !== s2[i] && indices.push(i) > 2) return false;
23+
}
24+
return s1[indices[0]] === s2[indices[1]] && s1[indices[1]] === s2[indices[0]];
25+
};

0 commit comments

Comments
 (0)