205. Isomorphic Strings | 判断两个字符串形式是否相同
来源:互联网 发布:网络废旧电子产品回收 编辑:程序博客网 时间:2024/05/27 09:48
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.
Subscribe to see which companies asked this question.
思路:用两个hash数组,分别记录s和t的字符的下一个相同的位置,如果没有下一个相同的,则是当前位置,创造hash数组时从后往前遍历两个字符串的字符。
public class Solution { public boolean isIsomorphic(String s, String t) { if (s.length() == 0) {return true;}int hashs[] = new int[256];int hasht[] = new int[256];int len = s.length();int hashnexts[] = new int[s.length()];int hashnextt[] = new int[t.length()];hashnexts[len - 1] = hashnextt[len - 1] = len - 1;for (int i = len - 1; i >= 0; i--) {if (hashs[s.charAt(i)] > 0) {hashnexts[i] = hashs[s.charAt(i)];}if (hasht[t.charAt(i)] > 0) {hashnextt[i] = hasht[t.charAt(i)];}hashs[s.charAt(i)] = i;hasht[t.charAt(i)] = i;}for (int j = 0; j < len; j++) {if (hashnexts[j] != hashnextt[j]) {return false;}}return true;}}
0 0
- 205. Isomorphic Strings | 判断两个字符串形式是否相同
- LeetCode-Isomorphic Strings:判断两个字符串是否同构(hashtable实现)
- 205. Isomorphic Strings (同构字符串判断)
- 判断两个字符串是否相同
- 判断两个字符串重排后是否相同
- leetcode 205. Isomorphic Strings 同构字符串判断 + HashMap
- Isomorphic Strings 判断俩等长字符串对应位置出现的字母是否类比相等
- leetcode205---Isomorphic Strings(相同结构的字符串)
- 判断字符串是否相同
- sql 判断两个字符串是否存在相同的内容
- 判断两个字符串是否有相同的字符
- Arrays_Strings 判断两个字符串是否有相同的字符 @CareerCup
- ReferenceEquals()判断两个字符串是否指向相同的内存地址
- 1.4-判断两个字符串是否由相同字符组成
- 判断两个字符串是否由相同字符组成
- 判断两个字符串是否由相同的字符组成
- 判断两个字符串是否由相同的字符组成
- Java 判断两个字符串是否由相同的字符组成
- PgSQL · 特性分析 · pg_receivexlog工具解析
- 堆排序原理及算法实现(最大堆)
- CUDA系列学习(四)Parallel Task类型 与 Memory Allocation
- java自动装箱之Integer
- 使用IntelliJ IDEA开发SpringMVC网站(四)用户管理
- 205. Isomorphic Strings | 判断两个字符串形式是否相同
- 关于自动化部署平台的尝试
- 什么是多线程和多进程【转】
- GYM 100247 I. Meteor Flow(贪心)
- HDU2883kebab(离散化+最大流_任务分配)
- 获取当前时间和星期
- 数据库30条军规解读
- 参数文件GinsengSharedPerferences
- 音视频同步、网络抖动