- 浏览: 185311 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
给定两个字符串,判断它们是否属于同构词。我们借助一个map和一个set,map用来存放两个字符串中对应字符的关系,set用来存放map中的value。题目假设两个字符串长度相同,假设我们比较到了第i个字符,如果map中已经存放了key为s.chatAt(i)的键值对,我们就比较t.charAt(i)与get(s.charAt(i))是否相等,如果不等,就返回false,如果相等就继续搜索。如果map中没有存放key为s.charAt(i)的键值对,我们此时要判断map中的value是否有t.charAt(i),如果set中已经存在t.charAt(i), 直接返回false;否则将这一个键值对放入map中,并且把value放入set中。代码如下:
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.
给定两个字符串,判断它们是否属于同构词。我们借助一个map和一个set,map用来存放两个字符串中对应字符的关系,set用来存放map中的value。题目假设两个字符串长度相同,假设我们比较到了第i个字符,如果map中已经存放了key为s.chatAt(i)的键值对,我们就比较t.charAt(i)与get(s.charAt(i))是否相等,如果不等,就返回false,如果相等就继续搜索。如果map中没有存放key为s.charAt(i)的键值对,我们此时要判断map中的value是否有t.charAt(i),如果set中已经存在t.charAt(i), 直接返回false;否则将这一个键值对放入map中,并且把value放入set中。代码如下:
public class Solution { public boolean isIsomorphic(String s, String t) { Map<Character, Character> hm = new HashMap<Character, Character>(); Set<Character> set = new HashSet<Character>(); for(int i = 0; i < s.length(); i++) { if(hm.containsKey(s.charAt(i))) { if(hm.get(s.charAt(i)) != t.charAt(i)) return false; } else { if(set.contains(t.charAt(i))) { return false; } else { hm.put(s.charAt(i), t.charAt(i)); set.add(t.charAt(i)); } } } return true; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 270Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 273You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 390Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 379Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 504Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 569Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 483Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 673Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 476The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 435Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 584Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 592Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 430All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 907Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 935Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 607Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 698Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 863Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 791You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 731For a undirected graph with tre ...
相关推荐
python python_leetcode题解之205_Isomorphic_Strings.py
LeetCode 205的题目是“同构字符串”(Isomorphic Strings),要求判断两个字符串是否是同构的。如果字符串s可以通过字符替换得到字符串t,那么这两个字符串是同构的。所有出现的字符都必须用另一个字符替换,同时...
4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 ...
从标题和描述中可以看到,本文主要关注于LeetCode原题的总结,涵盖了162个题目,包括Find Peak Element、Isomorphic Strings、Group Shifted Strings等,这些题目都是Google Onsite面经中的常见题目。 下面将对这些...
标题 "LeetCode209 - Isomorphic Strings" 涉及到的是一个计算机科学与编程相关的算法问题,主要探讨字符串的映射关系。在描述中,“映射字符串”这一概念指出我们要研究的是如何通过一种映射规则来比较两个字符串...
本题解将深入探讨Python解冑LeetCode第205题——"同构字符串"(Isomorphic Strings)。 同构字符串是指两个字符串s和t,它们之间存在一个一对一的映射关系,即每个字符在s中都有一个特定的字符对应t中的另一个字符...
- Isomorphic Strings(同构字符串):一种判断两个字符串是否具有相同映射的方法。 数据结构相关题目包括: - Binary Tree(二叉树):涉及二叉树的遍历、深度、插入、搜索等基本操作。 - Binary Search(二分...
public class IsomorphicStrings { public static void main(String[] args) { String s = "egg"; String t = "add"; boolean isIsomorphic = isIsomorphic(s, t); System.out.println(isIsomorphic ? "是同构...
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
IsomorphicStrings_lc205 lowestCommonAncestor_lc235 Netease 2018 - 2019 年度网易Android、Java岗算法题目汇总 最大兴趣值(MaxInterest) 数塔问题(BalancedTower) 丰收(Harvest) 牛牛找工作(FindJob) 数对
205.Isomorphic Strings-| | | 217.包含重复的-| | | 219.包含重复的II-| | | 242.有效字谜-| | | 268.缺少编号-| | | 344.反向字符串-| | | 345.一个字符串的反向元音-| | | 349.两个数组的交集-| | | 374.猜测数字...