博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Isomorphic Strings
阅读量:4073 次
发布时间:2019-05-25

本文共 1984 字,大约阅读时间需要 6 分钟。

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.

Java代码:

public class Solution {    public boolean isIsomorphic(String s, String t) {        Map
map = new HashMap<>(); Set
keyset; for (int i = 0; i < s.length(); i++) { keyset = map.keySet(); if (keyset.contains(s.charAt(i))) { List
list = map.get(s.charAt(i)); list.add(i); map.put(s.charAt(i), list); } else { List
list = new ArrayList<>(); list.add(i); map.put(s.charAt(i), list); } } keyset = map.keySet(); Iterator ite = keyset.iterator(); while (ite.hasNext()) { Character tmp = (Character) ite.next(); List
list = map.get(tmp); if (list.size() > 1) { Integer val = list.get(0); for (int i = 1; i < list.size(); i++) { if (t.charAt(val) != t.charAt(list.get(i))) return false; } } } // 交换s t 重新来一遍 map.clear(); keyset.clear(); for (int i = 0; i < t.length(); i++) { keyset = map.keySet(); if (keyset.contains(t.charAt(i))) { List
list = map.get(t.charAt(i)); list.add(i); map.put(t.charAt(i), list); } else { List
list = new ArrayList<>(); list.add(i); map.put(t.charAt(i), list); } } keyset = map.keySet(); ite = keyset.iterator(); while (ite.hasNext()) { Character tmp = (Character) ite.next(); List
list = map.get(tmp); if (list.size() > 1) { Integer val = list.get(0); for (int i = 1; i < list.size(); i++) { if (s.charAt(val) != s.charAt(list.get(i))) return false; } } } return true; }}
 

转载地址:http://tvuni.baihongyu.com/

你可能感兴趣的文章
No.182 - LeetCode1325 - C指针的魅力
查看>>
Encoding Schemes
查看>>
带WiringPi库的交叉笔译如何处理二之软链接概念
查看>>
Java8 HashMap集合解析
查看>>
自定义 select 下拉框 多选插件
查看>>
linux和windows内存布局验证
查看>>
Linux常用统计命令之wc
查看>>
fastcgi_param 详解
查看>>
搞定Java面试中的数据结构问题
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>