Isomorphic Strings
2016-06-08 05:26
162 查看
这道题和word pattern几乎一样,但自己还是在做出了一次大的错误后,才意识到将set改成map来做,我们能不能从中理解到奥义了!!!
public boolean isIsomorphic(String s, String t) {
if (s == null) {
return true;
}
Map<Character, Integer> setS = new HashMap<>();
Map<Character, Integer> setT = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (setS.containsKey(s.charAt(i))) {
if (setT.containsKey(t.charAt(i))) {
if (setS.get(s.charAt(i)).equals(setT.get(t.charAt(i)))) {
continue;
} else {
return false;
}
} else {
return false;
}
} else {
if (setT.containsKey(t.charAt(i))) {
return false;
} else {
setS.put(s.charAt(i), i);
setT.put(t.charAt(i), i);
}
}
}
return true;
// Set<Character> setS = new HashSet<>();
// Set<Character> setT = new HashSet<>();
// for (int i = 0; i < s.length(); i++) {
// if (setS.contains(s.charAt(i))) {
// if (setT.contains(t.charAt(i))) {
// continue;
// } else {
// return false;
// }
// } else {
// if (setT.contains(t.charAt(i))) {
// return false;
// } else {
// setS.add(s.charAt(i));
// setT.add(t.charAt(i));
// }
// }
// }
// return true;
}
public boolean isIsomorphic(String s, String t) {
if (s == null) {
return true;
}
Map<Character, Integer> setS = new HashMap<>();
Map<Character, Integer> setT = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (setS.containsKey(s.charAt(i))) {
if (setT.containsKey(t.charAt(i))) {
if (setS.get(s.charAt(i)).equals(setT.get(t.charAt(i)))) {
continue;
} else {
return false;
}
} else {
return false;
}
} else {
if (setT.containsKey(t.charAt(i))) {
return false;
} else {
setS.put(s.charAt(i), i);
setT.put(t.charAt(i), i);
}
}
}
return true;
// Set<Character> setS = new HashSet<>();
// Set<Character> setT = new HashSet<>();
// for (int i = 0; i < s.length(); i++) {
// if (setS.contains(s.charAt(i))) {
// if (setT.contains(t.charAt(i))) {
// continue;
// } else {
// return false;
// }
// } else {
// if (setT.contains(t.charAt(i))) {
// return false;
// } else {
// setS.add(s.charAt(i));
// setT.add(t.charAt(i));
// }
// }
// }
// return true;
}
相关文章推荐
- 求Sn=a+aa+aaa+aaaa+……+aaa…aaa(n个a)
- Autofac IContainer 测试
- 乡土中国笔记
- linux设置密码规则
- UBUNTU下LAMP的配置
- linux修改密码
- C# 按指定数量从前面或者后面删除字符串
- SDUT 3554 无尽走廊 (动态规划) -- 解题报告
- 使用pscp实现Windows 和 Linux服务器间远程拷贝文件
- 勾股定理一日一证连载17
- Autofac.Integration.Owin
- Autofac.Integration.Mvc.Owin分析
- 框架 day48 BOS项目练习2(base dao/action,登陆,消息提示框,菜单按钮,修改密码,登陆拦截器)
- leecode_236 Lowest Common Ancestor of a Binary Tree
- Autofac.Integration.Web分析
- 针对js中li标签的获取
- Autofac.Integration.Mvc分析
- xml的SAX解析
- 利用css3和js原生实现鼠标移入移出模块透明度的变化
- [Redux] Fetching Data on Route Change