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.

秒小题好爽的说。

思路:保存两个字符串字母对应的字母,出现不一致就false

我的代码:

bool isIsomorphic(string s, string t) {
unordered_map<char, char> map1, map2;
for(int i = ; i < s.size(); ++i)
{
if(map1.find(s[i]) == map1.end() && map2.find(t[i]) == map2.end())
{
map1[s[i]] = t[i]; map2[t[i]] = s[i];
}
else if(map1[s[i]] != t[i] || map2[t[i]] != s[i])
return false;
else;
}
return true;
}

网上C版本代码 免去了查找 更快

bool isIsomorphic(char* s, char* t) {
char mapST[] = { };
char mapTS[] = { };
size_t len = strlen(s);
for (int i = ; i < len; ++i)
{
if (mapST[s[i]] == && mapTS[t[i]] == )
{
mapST[s[i]] = t[i];
mapTS[t[i]] = s[i];
}
else
{
if (mapST[s[i]] != t[i] || mapTS[t[i]] != s[i])
return false;
}
}
return true;
}

【leetcode】Isomorphic Strings(easy)的更多相关文章

  1. 【leetcode】Isomorphic Strings

    题目简述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ...

  2. 【leetcode】Happy Number(easy)

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  3. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  5. 【leetcode】Remove Element (easy)

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  6. 【leetcode】Min Stack(easy)

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  7. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  8. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  9. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

随机推荐

  1. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法

    1.错误:  Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  2. springMVC之国际化

    1.工程结构 2.jar包 3.配置文件spring-config.xml,springMVC配置文件 <?xml version="1.0" encoding=" ...

  3. Linux学习:用yum安装php,httpd,mysql

    见鸟哥的linux私房菜电子版832页.

  4. ajax验证登录注册

    <form id="form1" onsubmit="return false;"> <table id="login-table& ...

  5. Method Swizzling和AOP(面向切面编程)实践

    Method Swizzling和AOP(面向切面编程)实践 参考: http://www.cocoachina.com/ios/20150120/10959.html 上一篇介绍了 Objectiv ...

  6. 对象的constructor属性

    对象的constructor属性, 最初是用来标识对象类型的. 比如 ,我们定义一个 Person类,然后实例化两个对象. function Person(name, age, job){this.n ...

  7. php写入txt换行符

    1.问题 写入txt文件想换行,老是直接输出了\r\n. 2.解决 要用双引号对\r\n进行解释,否则php会直接当字符输出. 3.例子 要求:往test.txt文本每一行后面加abc $a=file ...

  8. 跟着百度学PHP[4]OOP面对对象编程-12-对象接口技术(interface)

    PHP与大多数面向对象编程语言一样,不支持多重继承.也就是说每个类只能继承一个父类. 接口正是解决每个类只能继承一个父类这个问题的 接口用什么权限,继承的那个方法也要使用什么权限. 接口的声明使用:i ...

  9. linux kernel i2c底层代码跟踪

    其实跟上次跟的平台总线有关 在arch/arm/mach-mx6/board-mx6q_sabresd.c 文件中 static void __init mx6_sabresd_board_init( ...

  10. BZOJ 1461: 字符串的匹配

    Description 同上题. Sol KMP+树状数组. 写这题的时候我灰常naive...不管了...直接贴代码... Code /******************************* ...