leetcode_205题——Isomorphic Strings(用的map)

Isomorphic Strings

Total Accepted: 5891 Total Submissions: 24698My Submissions

Question Solution

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.

Hide Tags

Hash Table

Have you met this question in a real interview?

Yes

这道题需要对于字符串s和t,s中作为键,t中作为相对应的值,有两点,.s中不同的键对应的值不同,相同的键对应的值需要相同

#include<iostream>
#include<string>
#include <map>
#include <utility>
using namespace std;

bool isIsomorphic(string s, string t) {
	map<char,char> temp_map;
	int len=s.size();
	temp_map.insert(make_pair(s[0],t[0]));
	for(int i=1;i<len;i++)
	{
		int a=s[i];
		int b=t[i];
		if(temp_map.count(s[i])==1)
		{
			if(temp_map[s[i]]!=t[i])
				return false;
		}
		else
		{
			for(map<char,char>::iterator i=temp_map.begin();i!=temp_map.end();i++)
			{
				if(i->second==b)
					return false;
			}
			temp_map.insert(make_pair(s[i],t[i]));
		}
	}
	return true;
}
int main()
{
	string str1="ab";
	string str2="ca";
	cout<<isIsomorphic(str1,str2)<<endl;
}

  

时间: 2024-08-04 14:05:55

leetcode_205题——Isomorphic Strings(用的map)的相关文章

leetcode 204/187/205 Count Primes/Repeated DNA Sequences/Isomorphic Strings

一:leetcode 204 Count Primes 题目: Description: Count the number of prime numbers less than a non-negative number, n 分析:此题的算法源码可以参看这里,http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 代码: class Solution { public: int countPrimes(int n) { // 求小于一个数n的素数个

LeetCode:Isomorphic Strings

1.题目名称 Isomorphic Strings(同构的字符串) 2.题目地址 https://leetcode.com/problems/isomorphic-strings/ 3.题目内容 英文: 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 occurre

205. Isomorphic Strings - LeetCode

Question 205. Isomorphic Strings Solution 题目大意:判断两个字符串是否具有相同的结构 思路:构造一个map,存储每个字符的差,遍历字符串,判断两个两个字符串中相同位置字符的差是否相同 Java实现: public boolean isIsomorphic(String s, String t) { Map<String, Integer> map = new HashMap<>(); for (int i=0; i<s.length(

LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)

翻译 给定两个字符串s和t,决定它们是否是同构的. 如果s中的元素被替换可以得到t,那么称这两个字符串是同构的. 在用一个字符串的元素替换另一个字符串的元素的过程中,所有字符的顺序必须保留. 没有两个字符可以被映射到相同的字符,但字符可以映射到该字符本身. 例如, 给定"egg","add",返回真. 给定"foo","bar",返回假. 给定"paper","title",返回真. 批

205. Isomorphic Strings (Map)

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.

LeetCode之“散列表”: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 c

[LeetCode-JAVA] 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 characte

【leetcode】Isomorphic Strings(easy)

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.

leetcode笔记: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 chara