205. Isomorphic Strings
2016-03-18 21:19
453 查看
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
return true.
Given
return false.
Given
return true.
Note:
You may assume both s and t have the same length.
Subscribe to see which companies asked this question
其实用HashMap就好,可惜当年刚开始写不知道这玩意啊……就是每读一个s字母,就去找相对应的t中的字母,如果没找到就存起来,找到了就和已经存的比较,有冲突就报错就好。
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.
Subscribe to see which companies asked this question
其实用HashMap就好,可惜当年刚开始写不知道这玩意啊……就是每读一个s字母,就去找相对应的t中的字母,如果没找到就存起来,找到了就和已经存的比较,有冲突就报错就好。
public class Solution { <span style="white-space:pre"> </span>public boolean isIsomorphic(String s, String t) { <span style="white-space:pre"> </span>int[] a = new int[200]; <span style="white-space:pre"> </span>int[] b = new int[200]; <span style="white-space:pre"> </span>int s1 = 0; <span style="white-space:pre"> </span>int s2 = 0; <span style="white-space:pre"> </span>for (s1 = 0; s1 < s.length(); s1++) { <span style="white-space:pre"> </span>if (a[s.charAt(s1) - ' '] == 0) { <span style="white-space:pre"> </span>a[s.charAt(s1) - ' '] = t.charAt(s1) - ' '; <span style="white-space:pre"> </span>continue; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>if (a[s.charAt(s1) - ' '] != t.charAt(s1) - ' ') <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>for (s1 = 0; s1 < t.length(); s1++) { <span style="white-space:pre"> </span>if (b[t.charAt(s1) - ' '] == 0) { <span style="white-space:pre"> </span>b[t.charAt(s1) - ' '] = s.charAt(s1) - ' '; <span style="white-space:pre"> </span>continue; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>if (b[t.charAt(s1) - ' '] != s.charAt(s1) - ' ') <span style="white-space:pre"> </span>return false; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>return true; <span style="white-space:pre"> </span>} }
相关文章推荐
- Apache Kafka编程入门指南:Producer
- 读《java夜未眠》
- 5. Java引用与C++区别
- Android好用的okhttp网络封装(get,post,上传,下载)
- 【C++】《C++标准程序库》小结七章-迭代器
- 宏和函数的比较(C/C++)
- 190. Reverse Bits
- Struts2 注解模式(范例)
- PowerShell Studio 2016
- Problem E: 指针引出奇数因子
- python变量
- C#解leetcode 238. Product of Array Except Self
- 223. Rectangle Area
- 【暴力】HDU4858项目管理
- 219. Contains Duplicate II
- 给未来程序员的15个顶级职业建议 作者:程序媛
- 对于actionbar合并之后再次展开的问题--安卓开发(2)
- Libjingle综述
- 安卓控件布局
- Ural 2072 Kirill the Gardener 3