Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5274639

Browse files
check in source code
1 parent 2e1d59b commit 5274639

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace IsomorphicWords
9+
{
10+
/*
11+
Leetcode 205: Isomorphic Strings
12+
https://leetcode.com/problems/isomorphic-strings/description/
13+
*
14+
Given two strings s and t, determine if they are isomorphic.
15+
Two strings are isomorphic if the characters in s can be replaced to get t.
16+
All occurrences of a character must be replaced with another character while
17+
preserving the order of characters.
18+
19+
No two characters may map to the same character but a character may map to itself.
20+
Example 1:
21+
Input: s = "egg", t = "add"
22+
Output: true
23+
Example 2:
24+
Input: s = "foo", t = "bar"
25+
Output: false
26+
Example 3:
27+
Input: s = "paper", t = "title"
28+
Output: true
29+
Note:
30+
You may assume both s and t have the same length.
31+
*/
32+
class Program
33+
{
34+
const int SIZE = 256;
35+
36+
static void Main(string[] args)
37+
{
38+
RunTestcase1();
39+
RunTestcase2();
40+
RunTestcase3();
41+
RunTestcase4();
42+
}
43+
44+
public static void RunTestcase1()
45+
{
46+
var result = IsIsomorphic("egg","add");
47+
Debug.Assert(result);
48+
}
49+
50+
public static void RunTestcase2()
51+
{
52+
var result = IsIsomorphic("foo","bar");
53+
Debug.Assert(!result);
54+
}
55+
56+
public static void RunTestcase3()
57+
{
58+
var result = IsIsomorphic("paper", "title");
59+
Debug.Assert(result);
60+
}
61+
62+
/// <summary>
63+
/// No two characters may map to the same character but a character may map to itself
64+
/// </summary>
65+
public static void RunTestcase4()
66+
{
67+
var result = IsIsomorphic("pa", "qq");
68+
Debug.Assert(!result);
69+
}
70+
71+
/// <summary>
72+
/// Julia wrote the code after mock interivew of May 29, 2018 8:00 AM
73+
/// </summary>
74+
/// <param name="s"></param>
75+
/// <param name="t"></param>
76+
/// <returns></returns>
77+
public static bool IsIsomorphic(string s, string t)
78+
{
79+
if (s == null || t == null || s.Length != t.Length)
80+
{
81+
return false;
82+
}
83+
84+
var mapping = new int[SIZE]; // a - z mapping index from 1 to 26
85+
var mapUsed = new bool[SIZE];
86+
87+
var length = s.Length;
88+
89+
int index = 0;
90+
while (index < length)
91+
{
92+
var sChar = s[index];
93+
var tChar = t[index];
94+
95+
index++;
96+
97+
var sIndex = toIndex(sChar); //sChar - 'a' + 1;
98+
var tIndex = toIndex(tChar); //tChar - 'a' + 1;
99+
100+
var mapToIndex = mapping[sIndex];
101+
var hasMapping = mapToIndex != 0;
102+
103+
if (hasMapping)
104+
{
105+
if (tIndex == mapToIndex)
106+
{
107+
continue;
108+
}
109+
else
110+
{
111+
return false;
112+
}
113+
}
114+
else
115+
{
116+
//No two characters may map to the same character but a character may map to itself
117+
if (mapUsed[tIndex])
118+
{
119+
return false;
120+
}
121+
122+
mapping[sIndex] = tIndex;
123+
mapUsed[tIndex] = true;
124+
}
125+
}
126+
127+
return true;
128+
}
129+
130+
private static int toIndex(char c)
131+
{
132+
return c - '0円' + 1;
133+
}
134+
}
135+
}

‎Leetcode 205 Isomorphic strings.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace IsomorphicWords
9+
{
10+
/*
11+
Leetcode 205: Isomorphic Strings
12+
https://leetcode.com/problems/isomorphic-strings/description/
13+
*
14+
Given two strings s and t, determine if they are isomorphic.
15+
Two strings are isomorphic if the characters in s can be replaced to get t.
16+
All occurrences of a character must be replaced with another character while
17+
preserving the order of characters.
18+
19+
No two characters may map to the same character but a character may map to itself.
20+
Example 1:
21+
Input: s = "egg", t = "add"
22+
Output: true
23+
Example 2:
24+
Input: s = "foo", t = "bar"
25+
Output: false
26+
Example 3:
27+
Input: s = "paper", t = "title"
28+
Output: true
29+
Note:
30+
You may assume both s and t have the same length.
31+
*/
32+
class Program
33+
{
34+
const int SIZE = 256;
35+
36+
static void Main(string[] args)
37+
{
38+
RunTestcase1();
39+
RunTestcase2();
40+
RunTestcase3();
41+
RunTestcase4();
42+
}
43+
44+
public static void RunTestcase1()
45+
{
46+
var result = IsIsomorphic("egg","add");
47+
Debug.Assert(result);
48+
}
49+
50+
public static void RunTestcase2()
51+
{
52+
var result = IsIsomorphic("foo","bar");
53+
Debug.Assert(!result);
54+
}
55+
56+
public static void RunTestcase3()
57+
{
58+
var result = IsIsomorphic("paper", "title");
59+
Debug.Assert(result);
60+
}
61+
62+
/// <summary>
63+
/// No two characters may map to the same character but a character may map to itself
64+
/// </summary>
65+
public static void RunTestcase4()
66+
{
67+
var result = IsIsomorphic("pa", "qq");
68+
Debug.Assert(!result);
69+
}
70+
71+
/// <summary>
72+
/// Julia wrote the code after mock interivew of May 29, 2018 8:00 AM
73+
/// </summary>
74+
/// <param name="s"></param>
75+
/// <param name="t"></param>
76+
/// <returns></returns>
77+
public static bool IsIsomorphic(string s, string t)
78+
{
79+
if (s == null || t == null || s.Length != t.Length)
80+
{
81+
return false;
82+
}
83+
84+
var mapping = new int[SIZE]; // a - z mapping index from 1 to 26
85+
var mapUsed = new bool[SIZE];
86+
87+
var length = s.Length;
88+
89+
int index = 0;
90+
while (index < length)
91+
{
92+
var sChar = s[index];
93+
var tChar = t[index];
94+
95+
index++;
96+
97+
var sIndex = toIndex(sChar); //sChar - 'a' + 1;
98+
var tIndex = toIndex(tChar); //tChar - 'a' + 1;
99+
100+
var mapToIndex = mapping[sIndex];
101+
var hasMapping = mapToIndex != 0;
102+
103+
if (hasMapping)
104+
{
105+
if (tIndex == mapToIndex)
106+
{
107+
continue;
108+
}
109+
else
110+
{
111+
return false;
112+
}
113+
}
114+
else
115+
{
116+
//No two characters may map to the same character but a character may map to itself
117+
if (mapUsed[tIndex])
118+
{
119+
return false;
120+
}
121+
122+
mapping[sIndex] = tIndex;
123+
mapUsed[tIndex] = true;
124+
}
125+
}
126+
127+
return true;
128+
}
129+
130+
private static int toIndex(char c)
131+
{
132+
return c - '0円' + 1;
133+
}
134+
}
135+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /