[JavaScript] Easy to understand - 2 solutions
For this problem, the key point is "at most one string swap on exactly one of the strings" which means:\n- the count of diff characters between the 2 strings mu
For this problem, the key point is "at most one string swap on exactly one of the strings" which means:\n- the count of diff characters between the 2 strings must be 0 or 2\n- the diff characters must be the same\n\nAfter we got this, we could have our solutions easily.\n\n## SOLUTION 1\n\nThis is a typical solution. We check the sequence and counting the diff characters and check that for result.\n\n```js\nconst areAlmostEqual = (s1, s2) => {\n  const BASE = 97;\n  const dict1 = new Uint8Array(26);\n  const dict2 = new Uint8Array(26);\n\n  let diff = 0;\n  for (let i = 0; i < s1.length; ++i) {\n    ++dict1[s1.charCodeAt(i) - BASE];\n    ++dict2[s2.charCodeAt(i) - BASE];\n    s1[i] !== s2[i] && ++diff;\n  }\n\n  let flag = true;\n  for (let i = 0; i < 26; ++i) {\n    if (dict1[i] !== dict2[i]) { flag = false; break; }\n  }\n\n  return flag && (diff === 2 || diff === 0);\n};\n```\n\n## SOLUTION 2\n\nThis is an optimization for solution 1.\nTo get the result, we don\'t need the information of whole characters but only the information of diff characters.\nAnd we could return early if the diff count is more than 2.\n\n```js\nconst areAlmostEqual = (s1, s2) => {\n  let diff = 0;\n  let di1 = 0;\n  let di2 = 0;\n\n  for (let i = 0; i < s1.length; ++i) {\n    if (s1[i] === s2[i]) continue;\n    if (++diff > 2) return false;\n    diff === 1 ? (di1 = i) : (di2 = i);\n  }\n\n  return (diff === 0 || diff === 2) && s1[di1] === s2[di2] && s1[di2] === s2[di1];\n};\n```
['JavaScript']