LeetCode 同构字符串算法题解 All In One
LeetCode 同构字符串算法题解 All In One
205. 同构字符串
- 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/
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://leetcode.com/study-plan/leetcode-75/?progress=xl5bhjoe
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17011197.html
未经授权禁止转载,违者必究!