Spot look-alike domains

Phishers register names that differ from a real one by a single character, or that swap letters for similar digits, hoping readers will not notice.

What to build

Flag sites that look like a trusted site but are not it.

Requirements

  • Line 1: trusted sites. Each remaining line is a site to check (already just a name like bank-examp1e.com).
  • First normalise a name: lower case, then replace digit 0 with o, 1 with l, 3 with e, 5 with s.
  • A checked site is trusted if it equals a trusted site exactly. Otherwise it is a lookalike of X if, after normalising both, the two names are equal or differ by exactly one edit (one character inserted, deleted or replaced). Otherwise it is unrelated.
  • Check the trusted sites in input order and report the first match.

Input

As described.

Output

One line per site.

Constraints

Up to 10 trusted sites and 30 sites to check, each up to 60 characters.

Example

Input:

bank-example.com
bank-example.com
bank-examp1e.com

Output:

trusted
lookalike of bank-example.com

Why it works

Digit swaps and single-character edits are the cheapest ways to make a name that passes a quick glance; normalising the swaps and allowing one edit catches both, while an exact match is still required for trust.

Examples

Example 1 · Input
bank-example.com
bank-example.com
bank-examp1e.com
Expected output
trusted
lookalike of bank-example.com
Example 2 · Input
paylink.example
pay1ink.example
completely-other.test
Expected output
lookalike of paylink.example
unrelated

Hints

Hint 1

One edit means: same length with one different character, or lengths differ by one and deleting one character from the longer gives the shorter.

Solution

Show one correct solution (try it yourself first)
const trusted = prompt().trim().split(/\s+/);
const norm = (s) => s.toLowerCase().replace(/0/g, "o").replace(/1/g, "l").replace(/3/g, "e").replace(/5/g, "s");
function oneEditOrSame(a, b) {
  if (a === b) return true;
  if (Math.abs(a.length - b.length) > 1) return false;
  if (a.length === b.length) {
    let diff = 0;
    for (let k = 0; k < a.length; k++) if (a[k] !== b[k]) diff++;
    return diff === 1;
  }
  const [long, short] = a.length > b.length ? [a, b] : [b, a];
  for (let k = 0; k < long.length; k++) if (long.slice(0, k) + long.slice(k + 1) === short) return true;
  return false;
}
let line = prompt();
while (line !== null) {
  const s = line.trim();
  if (s) {
    if (trusted.includes(s)) console.log("trusted");
    else {
      const hit = trusted.find((t) => oneEditOrSame(norm(s), norm(t)));
      console.log(hit ? `lookalike of ${hit}` : "unrelated");
    }
  }
  line = prompt();
}

▶ Open this solution in the compiler, then press Visualize to watch it run step by step

Lesson: Phishing and social engineering →