Skip to content

Instantly share code, notes, and snippets.

@suguanYang
Last active July 6, 2021 09:28
Show Gist options
  • Save suguanYang/31fc7d63b6482eb6725003c787ca9e8b to your computer and use it in GitHub Desktop.
Save suguanYang/31fc7d63b6482eb6725003c787ca9e8b to your computer and use it in GitHub Desktop.
A fast way to check if 2 string are composed of same characters.
const alphas = {
  a: 101,
  b: 2,
  c: 3,
  d: 5,
  e: 7,
  f: 11,
  g: 13,
  h: 17,
  i: 19,
  j: 23,
  k: 29,
  l: 31,
  m: 37,
  n: 41,
  o: 43,
  p: 47,
  q: 53,
  r: 59,
  s: 61,
  t: 67,
  u: 71,
  v: 73,
  w: 79,
  x: 83,
  y: 89,
  z: 97,
};

function is2StringSame(a, b) {
  if (a.length !== b.length) {
    return false;
  }
  let haskKeya = 1;
  let haskKeyb = 1;
  for (let s of a) {
    haskKeya *= alphas[s];
  }
  for (let s of b) {
    haskKeyb *= alphas[s];
  }
  return haskKeya === haskKeyb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment