码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode Isomorphic Strings

时间:2015-05-07 08:46:25      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

 

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
 1 // 08:18
 2 class Solution {
 3 public:
 4     bool isIsomorphic(string s, string t) {
 5         int slen = s.size();
 6         int tlen = t.size();
 7         if (slen != tlen) {
 8             return false;
 9         }
10         char tbl[256] = {0};
11         bool used[256] = {0};
12         for (int i=0; i<slen; i++) {
13             char ch = s[i];
14             if (tbl[ch] == 0 && !used[t[i]]) {
15                 tbl[ch] = t[i];
16                 used[t[i]] = true;
17                 continue;
18             }
19             if (tbl[ch] != t[i]) {
20                 return false;
21             }
22         }
23         return true;
24     }
25 };

 

 

LeetCode Isomorphic Strings

标签:

原文地址:http://www.cnblogs.com/lailailai/p/4483803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!