app开发者平台在数字化时代的重要性与发展趋势解析
859
2022-10-09
205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
For example, Given “egg”, “add”, return true.
Given “foo”, “bar”, return false.
Given “paper”, “title”, return true.
Note: You may assume both s and t have the same length.
public class Solution { //test case: "egg", "add" public boolean isIsomorphic(String s, String t) { //init check if(s==null || t==null) return false; if(s.length() != t.length()) return false; Map public class Solution { public boolean isIsomorphic(String s, String t) { for (int i = 0; i < s.length(); i++) { if (s.indexOf(s.charAt(i)) != t.indexOf(t.charAt(i))) return false; } return true; } } /** * @param {string} s * @param {string} t * @return {boolean} */var isIsomorphic = function(s, t) { const sLen = s.length const tLen = t.length if (sLen !== tLen) { return false } if (!sLen && !tLen) { return true } const maps = new Map() const mapt = new Map() for (let i = 0; i < sLen; i++) { const sChar = s[i] const tChar = t[i] if (!maps.has(sChar)) { maps.set(sChar, tChar) } else { if (maps.get(sChar) !== tChar) { return false } } if (!mapt.has(tChar)) { mapt.set(tChar, sChar) } else { if (mapt.get(tChar) !== sChar) { return false } } } return true}; /** * @param {string} s * @param {string} t * @return {boolean} */var isIsomorphic = function(s, t) { for(var i = 0; i< s.length; i++) { if(s.indexOf(s[i]) !== t.indexOf(t[i])) return false } return true;};
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~