Isomorphic Strings Solutions in C++
Number 205
Difficulty Easy
Acceptance 39.8%
Link LeetCode
Other languages Go
Solutions
C++ solution by haoel/leetcode
// Source : https://leetcode.com/problems/isomorphic-strings/// Author : Hao Chen// Date : 2015-06-09class Solution {public:bool isIsomorphic(string s, string t) {if (s.size()!=t.size()) return false;const int MAXCHAR = 256;char maps[MAXCHAR]={0}, mapt[MAXCHAR]={0};//memset(maps, 0, sizeof(maps));//memset(mapt, 0, sizeof(mapt));for(int i=0; i<s.size(); i++){if(maps[s[i]] == 0 && mapt[t[i]] == 0){maps[s[i]] = t[i];mapt[t[i]] = s[i];continue;}if(maps[s[i]] == t[i] && mapt[t[i]] == s[i]) {continue;}return false;}return true;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/isomorphic-strings/description//// Author : liuyubobobo/// Time : 2018-06-01#include <iostream>using namespace std;/// Mapping/// Time Complexity: O(len(s))/// Space Complexity: O(len of charset)class Solution {public:bool isIsomorphic(string s, string t) {if(s.size() != t.size())return false;int map[256];memset(map, -1, sizeof(map));bool mapped[256];memset(mapped, false, sizeof(mapped));for(int i = 0 ; i < s.size() ; i ++){if(map[s[i]] == -1){if(mapped[t[i]])return false;map[s[i]] = t[i];mapped[t[i]] = true;}else if(map[s[i]] != t[i])return false;}return true;}};void print_bool(bool res){cout << (res ? "True" : "False") << endl;}int main() {print_bool(Solution().isIsomorphic("egg", "add"));print_bool(Solution().isIsomorphic("foo", "bar"));print_bool(Solution().isIsomorphic("paper", "title"));print_bool(Solution().isIsomorphic("aa", "ab"));return 0;}