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

本文共 1397 字,大约阅读时间需要 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.

思路:

所有s中的每个字母在对应位置都可以被t中同一个字母替换,paper的p被title的t替换了,那么下一次出现p的地方一定对应t

从1到length遍历,建立一个hash表用于标识出现过的字母,这里需要注意,组成字符串的字符可以是ascii码中的任意,ascii码由0~255,所以建立两个int[256]用来记录    最终目的就是验证这次a对应b下次出现a的时候对应的还得是b

int[] apls = new int[256];int[] aplt = new int[256];    for(int i = 0;i < s.length();i++){          if(apls[s.charAt(i)]!=aplt[t.charAt(i)])return false;          apls[s.charAt(i)] = i+1;          aplt[t.charAt(i)] = i+1;    }return true;
也可以将s字符和t的字符建立映射,但是光每对映射相同还不行,t中字符智能对应一种映射,他们是一对一映射

int[] apl = new int[256];        Set
set = new HashSet(); for(int i = 0;i < s.length();i++){ if(apl[s.charAt(i)] == 0){ if(set.add((int)t.charAt(i))){ apl[s.charAt(i)] = (int)t.charAt(i); }else return false; } else if(apl[s.charAt(i)]!=(int)t.charAt(i))return false; } return true;

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

你可能感兴趣的文章
基于“分形”编写的交互应用
查看>>
《融入动画技术的交互应用》主题博文推荐
查看>>
链睿和家乐福合作推出下一代零售业隐私保护技术
查看>>
Unifrax宣布新建SiFAB™生产线
查看>>
艾默生纪念谷轮™在空调和制冷领域的百年创新成就
查看>>
NEXO代币持有者获得20,428,359.89美元股息
查看>>
Piper Sandler为EverArc收购Perimeter Solutions提供咨询服务
查看>>
RMRK筹集600万美元,用于在Polkadot上建立先进的NFT系统标准
查看>>
JavaSE_day14 集合中的Map集合_键值映射关系
查看>>
异常 Java学习Day_15
查看>>
Mysql初始化的命令
查看>>
MySQL关键字的些许问题
查看>>
浅谈HTML
查看>>
css基础
查看>>
Servlet进阶和JSP基础
查看>>
servlet中的cookie和session
查看>>
过滤器及JSP九大隐式对象
查看>>
软件(项目)的分层
查看>>
菜单树
查看>>
Servlet的生命周期
查看>>