leetcode 205. 同构字符串(hash)
生活随笔
收集整理的这篇文章主要介绍了
leetcode 205. 同构字符串(hash)
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
给定两个字符串 s 和 t,判断它们是否是同构的。
如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。
所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
示例 1:
输入: s = “egg”, t = “add”
输出: true
代码
class Solution {public boolean isIsomorphic(String s, String t) {Map<Character,Character> map=new HashMap<>();Map<Character,Character> map2=new HashMap<>();for(int i=0;i<s.length();i++){if(map.containsKey(s.charAt(i))&&t.charAt(i)!=map.get(s.charAt(i))||map2.containsKey(t.charAt(i))&&s.charAt(i)!=map2.get(t.charAt(i)))//出现一对多的情况,说明无法匹配return false;map.put(s.charAt(i),t.charAt(i));//字符串s和t的字母相互映射map2.put(t.charAt(i),s.charAt(i));}return true;} }总结
以上是生活随笔为你收集整理的leetcode 205. 同构字符串(hash)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 女人梦到斧头是什么征兆
- 下一篇: leetcode 188. 买卖股票的最