205 Isomorphic Strings

链接:https://leetcode.com/problems/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.

Note:
You may assume both s and t have the same length.

这个问题实际实在两个字符串间建立映射,每个字符只能有一种映射,不能出现多个映射。比如foo,bar中第二个o映射了a,第三个o映射了r,出现了两种映射因此不满足题意。解题时可以考虑建立映射表,因为要比较s,t两个字符数组,可以建立两个映射表,用table1表示s到t的映射,用table2表示t到s的映射。当映射产生矛盾时则返回false。

bool isIsomorphic(char* s, char* t)
{int index=0;while(s[index]!='\0')index++;if(t[index]!='\0')return false;unsigned char *table1=(unsigned char *)malloc(128*sizeof(char));unsigned char *table2=(unsigned char *)malloc(128*sizeof(char));memset(table2,128,128*sizeof(char));memset(table1,128,128*sizeof(char));for(int i=0;i<index;i++){if(table1[s[i]]==128)table1[s[i]]=t[i];elseif(table1[s[i]]!=t[i]){free(table1);free(table2);return false;}if(table2[t[i]]==128)table2[t[i]]=s[i];elseif(table2[t[i]]!=s[i]){free(table1);free(table2);return false;}}free(table1);free(table2);return true;
}

字符用ASCII码表示,从0到127,共有128个。因此128的数组就以用来建立映射表,初始化时候只要值不是0到127都没问题。

205 Isomorphic Strings相关推荐

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

    翻译 给定两个字符串s和t,决定它们是否是同构的.假设s中的元素被替换能够得到t,那么称这两个字符串是同构的.在用一个字符串的元素替换还有一个字符串的元素的过程中.所有字符的顺序必须保留. 没有两个字 ...

  2. leetcode python3 简单题205. Isomorphic Strings

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零五题 (1)题目 英文: Given two strings s and t ...

  3. 205. Isomorphic Strings

    1,题目要求 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if th ...

  4. Easy-题目46:205. Isomorphic Strings

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

  5. leetcode之Isomorphic strings

    问题来源:Isomorphic strings 问题描述:给定两个字符串,判断它俩是否满足同构关系.同构关系表示两个字符串的字符能一一对应,例如title和paper构成同构关系,而foo和bar不构 ...

  6. hdu 6863 Isomorphic Strings

    Isomorphic Strings 题解:暴力枚举n的所有因子,判断是否合法.判断合法第一步:判断每段中各字符出现次数是否相同,第二部:判断每段的最小表示是否相同.因子还可以减枝. #include ...

  7. CodeForces - 985F Isomorphic Strings

    假如两个区间的26的字母出现的位置集合分别是 A1,B1,A2,B2,....., 我们再能找到一个排列p[] 使得 A[i] = B[p[i]] ,那么就可以成功映射了. 显然集合可以直接hash, ...

  8. [LeetCode] Isomorphic Strings - 字符串操作:数组计数字符个数问题

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

  9. C#LeetCode刷题之#205-同构字符串(Isomorphic Strings)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3770 访问. 给定两个字符串 s 和 t,判断它们是否是同构的. ...

最新文章

  1. 2010年的退休畅想
  2. linux 问题 value too large for defined data type 解决方案
  3. xp下msn8.5无法安装的解决办法
  4. .net生成excel并弹出保存提示框(转载)
  5. mysql 表设计时的update_time自动更新
  6. boost::multiprecision模块cpp_bin_float_100相关的测试程序
  7. mysql clean up_svn之clean up操作
  8. Git常用命令使用大全
  9. 自定义viewgroup实现ArcMenu
  10. object.\__mro__
  11. php socket 小程序,实例详解微信小程序如何使用Socket
  12. Java-虚拟机-常量池的表现方式
  13. 手工清除severe.exe病毒
  14. CentOS8下vi编辑器常用命令
  15. kubectl edit 与kubectl rollout
  16. 【PostgreSQL逻辑复制数据同步到kafka】
  17. net start mysql:无法启动
  18. Application entry file “background.ts“ in the “**dist_electron\win-unpacked\resources\app.asar“ does
  19. 大数据学习笔记之一:Hadoop 常用指令集合与启动注意项
  20. 全国2010年10月考试电子商务与电子政务试题

热门文章

  1. 算法初级_Question7_最佳存款方案(java实现)
  2. 低版本IntelliJ IDEA升级到IntelliJ IDEA 2021.3.3
  3. 本特利二次表2300/20-00
  4. vue-cil创建vue项目
  5. 模糊综合评价法资料连接
  6. 【原创】我的MP3播放器--2007.12(搬家待修复)
  7. 如何管理机器学习模型
  8. 电子计算机按数字错乱,笔记本电脑数字键盘错乱怎么办 笔记本键盘按键错乱恢复方法...
  9. 基于大规模MIMO的低轨卫星通信系统
  10. nginx开启https配置