当前位置:网站首页>Isomorphic strings for leetcode topic resolution
Isomorphic strings for leetcode topic resolution
2022-06-23 08:43:00 【ruochen】
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.
Ensure a one-to-one mapping relationship , I.e. letters e Mapping a, Can no longer represent itself
public boolean isIsomorphic(String s, String t) {
if (s == null || t == null) {
return false;
}
if (s.length() != t.length()) {
return false;
}
Map<Character, Character> map = new HashMap<Character, Character>();
Set<Character> set = new HashSet<Character>();
for (int i = 0; i < s.length(); i++) {
char c1 = s.charAt(i);
char c2 = t.charAt(i);
// Ensure a one-to-one mapping relationship
if (map.containsKey(c1)) {
if (map.get(c1) != c2) {
return false;
}
} else {
if (set.contains(c2)) {
return false;
}
map.put(c1, c2);
set.add(c2);
}
}
return true;
}边栏推荐
- MySQL common skills
- 点云库pcl从入门到精通 第十章
- Chapter 1 open LDAP master-slave synchronization tower construction
- usb peripheral 驱动 - configfs
- Why do we say that the data service API is the standard configuration of the data midrange?
- How to sort a dictionary by value or key?
- Go 数据类型篇(二)之Go 支持的数据类型概述及布尔类型
- Focus! Ten minutes to master Newton convex optimization
- Set the time zone in ci4 (CodeIgniter 4)
- vector的深度剖析及模拟实现
猜你喜欢

Why do we say that the data service API is the standard configuration of the data midrange?

渲染效果图哪家好?2022最新实测(四)

6月《中國數據庫行業分析報告》發布!智能風起,列存更生
![[cloud computing] GFS ideological advantages and architecture](/img/98/2a4ef0ca805add24d431dac9808903.png)
[cloud computing] GFS ideological advantages and architecture

173. Binary Search Tree Iterator

What are the PCB characteristics inspection items?

点云库pcl从入门到精通 第十章

最常用的5中流ETL模式

目标检测中的多尺度特征结合方式

Qualcomm 9x07 two startup modes
随机推荐
APM performance monitoring practice of jubasha app
125. Valid Palindrome
驱动架构 & platform平台总线驱动模型
Linux Mysql安装
Easygbs cannot play video streams in webrtc format. What is the reason?
你有一串代码,但是不支持低版本Go时;判断Go版本号,您值得拥有!
Linux MySQL installation
Testing -- automated testing selenium (about API)
How to sort a dictionary by value or key?
Map interface and its sub implementation classes
Set the time zone in ci4 (CodeIgniter 4)
目标检测中的多尺度特征结合方式
6-闪耀的激光-CALayer 的应用
Data assets are king, analyzing the relationship between enterprise digital transformation and data asset management
There are some limitations in cluster expansion and contraction
528. Random Pick with Weight
XSS via host header
65. Valid Number
How to mine keywords and improve user experience before website construction?
usb peripheral 驱动 - configfs