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 — #1153 - String Transforms Into Another String - LeetCode JavaScript Solutions

Back to all solutions

#1153 - String Transforms Into Another String

Hard • View on LeetCode • View on GitHub
String Hash Table

Problem Description

Given two strings str1 and str2 of the same length, determine whether you can transform str1 into str2 by doing zero or more conversions.

In one conversion you can convert all occurrences of one character in str1 to any other lowercase English character.

Return true if and only if you can transform str1 into str2.

Solution

/**
 * @param {string} str1
 * @param {string} str2
 * @return {boolean}
 */
var canConvert = function(str1, str2) {
  if (str1 === str2) return true;

  const mapping = new Map();
  const uniqueCharsInStr2 = new Set();
  for (let i = 0; i < str1.length; i++) {
    const char1 = str1[i];
    const char2 = str2[i];

    if (mapping.has(char1)) {
      if (mapping.get(char1) !== char2) {
        return false;
      }
    } else {
      mapping.set(char1, char2);
    }

    uniqueCharsInStr2.add(char2);
  }

  return uniqueCharsInStr2.size < 26;
};
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.