`
hcx2013
  • 浏览: 83270 次
社区版块
存档分类
最新评论

Isomorphic Strings

 
阅读更多

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.

 

public class Solution {
    public boolean isIsomorphic(String s, String t) {
    	if (s.length() != t.length()) {
    		return false;
    	}
        HashMap<Character,Character> hashMapS = new HashMap<>();
        HashMap<Character,Character> hashMapT = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
			if (hashMapS.containsKey(s.charAt(i))) {
				if (hashMapS.get(s.charAt(i)) != t.charAt(i)) {
					return false;
				}
			} else {
				if (hashMapT.containsKey(t.charAt(i))) {
					return false;
				}
				hashMapS.put(s.charAt(i), t.charAt(i));
				hashMapT.put(t.charAt(i), s.charAt(i));
			}
		}
		return true;
    }
}

 

0
1
分享到:
评论

相关推荐

    Leetcode 同构字符串.php

    LeetCode 205的题目是“同构字符串”(Isomorphic Strings),要求判断两个字符串是否是同构的。如果字符串s可以通过字符替换得到字符串t,那么这两个字符串是同构的。所有出现的字符都必须用另一个字符替换,同时...

    Coding Interview In Java

    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 ...

    2018年最新Google Onsite面经总结1

    (3)162. Find Peak Element (2)205. Isomorphic Strings (3)249. Group Shifted Strin

    cpp-算法精粹

    Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram Evaluate Reverse Polish Notation Implement Stack using Queues 队列 ...

    LeetCode最全代码

    ...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....|-----|---------------- | --------------- |...

    leetcode209-LeetCode209_Isomorphic_Strings:映射字符串

    leetcode209 LeetCode209_Isomorphic_Strings 映射字符串

    招银网络java科技笔试题-WaytoInterview:JVM和设计模式和算法的快速浏览

    IsomorphicStrings_lc205 lowestCommonAncestor_lc235 Netease 2018 - 2019 年度网易Android、Java岗算法题目汇总 最大兴趣值(MaxInterest) 数塔问题(BalancedTower) 丰收(Harvest) 牛牛找工作(FindJob) 数对

    发帖源码java-leetcode-javascript-solutions:javascript中的leetcode问题的解决方案,位于以下

    205.Isomorphic Strings-| | | 217.包含重复的-| | | 219.包含重复的II-| | | 242.有效字谜-| | | 268.缺少编号-| | | 344.反向字符串-| | | 345.一个字符串的反向元音-| | | 349.两个数组的交集-| | | 374.猜测数字...

    Isomorphic Web Applications Universal Development With React

    The book is to teach you to think in a way that will make you successful when working with isomorphic architecture. Given React’s presence in the web community and the support React provides for ...

    Isomorphic Go_Code 源码

    Isomorphic Go_Code 源码 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Isomorphic JavaScript Web Development epub

    Isomorphic JavaScript Web Development 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Building Isomorphic JavaScript Apps

    Isomorphic JavaScript, often described as the holy grail of web application development, refers to running JavaScript code on both the browser client and web application server. This application ...

    前端开源库-isomorphic-loader

    前端开源库-isomorphic-loader同构加载器,Webpack同构加载器工具,使节点需要处理像服务器端渲染(SSR)的图像这样的文件。

    前端开源库-isomorphic-unfetch

    前端开源库-isomorphic-unfetch同构取消蚀刻,在客户端和服务器的取消蚀刻和节点提取之间切换。

    Isomorphic Go mobi

    Isomorphic Go 英文mobi 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Isomorphic Application Development with JavaScript azw3

    Isomorphic Application Development with JavaScript 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

    前端项目-isomorphic-git.zip

    前端项目-isomorphic-git,一个针对节点和浏览器的Git纯JavaScript实现!

    Isomorphic+JavaScript+Web+Development-Packt+Publishing(2017).pdf

    Chapter 1, Getting Started with Isomorphic Web Apps, describes what isomorphic apps are and how they differ from the conventional SPAs that are around. They will understand the main challenges that ...

    Isomorphic Go epub

    Isomorphic Go 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    isomorphic-go

    Isomorphic Go is the methodology to create isomorphic web applications using the Go programming language. Isomorphic web applications have the capability to reuse code across environments, increasing ...

Global site tag (gtag.js) - Google Analytics