Chinaunix首页 | 论坛 | 博客

AAA

  • 博客访问: 18426
  • 博文数量: 33
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 330
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 08:23
个人简介

好记性不如烂笔头,记录学习历程和随想云云,欢迎各位大虾拍砖

文章分类

全部博文(33)

文章存档

2015年(33)

我的朋友
最近访客

分类: C/C++

2015-05-04 21:03:57

【问题描述】

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.
【解决方案】

点击(此处)折叠或打开

  1. bool isIsomorphic(char* s, char* t)
  2. {
  3.     int len = strlen(s);
  4.     int i,x,y;
  5.     char hashs[128] = {0};
  6.     char hasht[128] = {0};

  7.     for(i = 0; i < len; i++)
  8.     {
  9.         x = s[i];
  10.         if(0 == hashs[x])
  11.         {
  12.             hashs[x] = t[i];
  13.         }
  14.         else if(hashs[x] != t[i])
  15.         {
  16.             return false;
  17.         }

  18.         /* 数组t也需要同样判断,反例如:"ab", "aa" */
  19.         y = t[i];
  20.         if(0 == hashs[x])
  21.         {
  22.             hasht[y] = t[i];
  23.         }
  24.         else if(hasht[y] != t[i])
  25.         {
  26.             return false;
  27.         }
  28.     }

  29.     return true;
  30. }

阅读(292) | 评论(0) | 转发(0) |
0

上一篇:KMP

下一篇:Happy Number

给主人留下些什么吧!~~