리트코드/easy
[리트코드] 205. Isomorphic Strings - js
bbeyak
2023. 9. 2. 12:42
반응형
1. 문제
https://leetcode.com/problems/isomorphic-strings/
Isomorphic Strings - LeetCode
Can you solve this real interview question? Isomorphic Strings - Given two strings s and t, determine if they are isomorphic. Two strings s and t are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replace
leetcode.com
2. 코드
이 문제는 설명을 이해하는데 조금 오래 걸렸다.
Isomorphic은 동형사상이라는 뜻으로, 두 String의 형태가 닮아있는지 여부를 확인하는 문제이다.
예를 들어 다음은 동형관계다.
s = "egg", t = "add"
알파벳이 등장하는 순서대로 번호를 매기면 122, 122로 구조가 같다.
반대로 banana와 canada는 등장 순서대로 번호를 매기면 123232와 123242 이므로 동형관계가 아니다.
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isIsomorphic = function (s, t) {
if (s.length != t.length) return false;
let sArr = [];
let tArr = [];
for (let i = 0; i < s.length; i++) {
sArr.push(s.indexOf(s[i]));
tArr.push(t.indexOf(t[i]));
if (sArr[i] != tArr[i]) return false;
}
return true;
};
반응형