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

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

题目:

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.

链接:  

2/24/2017

需要用2个hashmap,aa - > ab是不成立的

1 public class Solution { 2     public boolean isIsomorphic(String s, String t) { 3         if (s.length() != t.length()) return false; 4  5         HashMap
h1 = new HashMap
(); 6 HashMap
h2 = new HashMap
(); 7 char a, b; 8 9 for (int i = 0; i < s.length(); i++) {10 a = s.charAt(i);11 b = t.charAt(i);12 if (h1.containsKey(a)) {13 if (h1.get(a) != b) return false;14 h1.put(a, b);15 } else {16 h1.put(a, b);17 }18 if (h2.containsKey(b)) {19 if (h2.get(b) != a) return false;20 } else {21 h2.put(b, a);22 }23 }24 return true;25 26 }27 }

别人的好算法可以用bitmap,留给二刷。

转载于:https://www.cnblogs.com/panini/p/6440840.html

你可能感兴趣的文章
Java与算法之(2) - 快速排序
查看>>
Windows之IOCP
查看>>
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------&gt;Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>