xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 同构字符串算法题解 All In One

LeetCode 同构字符串算法题解 All In One

205. 同构字符串

  1. Isomorphic Strings

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-12-29
 * @modified
 *
 * @description 205. Isomorphic Strings
 * @description 205. 同构字符串
 * @difficulty Easy
 * @ime_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/isomorphic-strings/
 * @link https://leetcode.cn/problems/isomorphic-strings/
 * @solutions
 *
 * @best_solutions
 *
 */

export {};

const log = console.log;

function isIsomorphic(s: string, t: string): boolean {
  let result = true;
  const obj1 = {};
  const obj2 = {};
  for(let i = 0; i < s.length; i++) {
    if(!obj1[s[i]]) {
      obj1[s[i]] = t[i];
    }
    if(!obj2[t[i]]) {
      obj2[t[i]] = s[i];
    }
    // 双向字典 比较
    if(obj1[s[i]] !== t[i] || obj2[t[i]] !== s[i]) {
      result = false;
      break;
    }
  }
  return result;
};

/*
s =
"badc"
t =
"baba"

false ???
*/


// 测试用例 test cases
const testCases: TestCasesI = [
  {
    inputs:["badc", "baba"],
    result: false,
    desc: 'value equal to false',
  },
];


for (const [i, testCase] of testCases.entries()) {
  const [s, t] = testCase.inputs;
  const result = isIsomorphic(s, t);
  log(`test case i result: \n`, result === testCase.result  ? `passed ✅` : `failed ❌`, result);
  // log(`test case i =`, testCase);
}

// npx ts-node ./205\ isomorphic-strings.ts


https://leetcode.com/problems/isomorphic-strings/

https://leetcode.cn/problems/isomorphic-strings/

refs

https://leetcode.com/study-plan/leetcode-75/?progress=xl5bhjoe



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(15)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
历史上的今天:
2021-12-29 v-charts bug All In One
2021-12-29 企业微信如何设置用户当前状态 All In One
2020-12-29 Windows 10 Emoji shortcuts All In One
2020-12-29 es6 curry function
2020-12-29 HTML <keygen> 标签(👎 已废弃)
2020-12-29 How to get the real screen size(screen resolution) by using js All In One
2020-12-29 CSS :nth-of-type() bug
点击右上角即可分享
微信分享提示