GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #1554 - Strings Differ by One Character - LeetCode JavaScript Solutions

Back to all solutions

#1554 - Strings Differ by One Character

Medium • View on LeetCode • View on GitHub
String Hash Table Hash Function Rolling Hash

Problem Description

Given a list of strings dict where all the strings are of the same length.

Return true if there are 2 strings that only differ by 1 character in the same index, otherwise return false.

Solution

/**
 * @param {string[]} dict
 * @return {boolean}
 */
var differByOne = function(dict) {
  const m = dict[0].length;

  for (let pos = 0; pos < m; pos++) {
    const patterns = new Set();

    for (const word of dict) {
      const pattern = word.slice(0, pos) + '*' + word.slice(pos + 1);

      if (patterns.has(pattern)) {
        return true;
      }

      patterns.add(pattern);
    }
  }

  return false;
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.