-
Notifications
You must be signed in to change notification settings - Fork 56
/
13.IfStrIsIsomorphic.cs
112 lines (95 loc) · 2.04 KB
/
13.IfStrIsIsomorphic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//Check if a string is isomorphic
using System;
using System.Collections.Generic;
using System.Threading;
namespace CheckIsomorphic
{
public class Checker
{
public Dictionary<char,char> DicA { get; private set;}
public Dictionary<char,char> DicB { get; private set;}
public Checker()
{
this.DicA = new Dictionary<char, char> ();
this.DicB = new Dictionary<char, char> ();
}
public bool CheckIsomorphic (string strA, string strB)
{
if(strA == null || strB == null)
return false;
if (strA.Length != strB.Length)
return false;
if (strA.Equals (strB))
return true;
//put pair into two dictionaries.
for (int i = 0; i < strA.Length; i++) {
char tempA = strA [i];
char tempB = strB [i];
if (!DicA.ContainsKey (tempA)) // must check if the key already existed
DicA.Add (tempA, tempB);
if (!DicB.ContainsKey (tempB))
DicB.Add (tempB, tempA);
}
//
for (int i = 0; i < strA.Length; i++) {
char tempA = strA [i];
char tempB = strB [i];
if (tempA != DicB [tempB])
return false;
if (tempB != DicA [tempA])
return false;
}
return true;
}
}
class MainClass
{
public static void Main (string[] args)
{
string strA = "abb";
string strB = "oxc";
Checker checker = new Checker ();
bool result = checker.CheckIsomorphic (strA, strB);
Console.WriteLine (result);
}
}
}
class SizeQueue<T>
{
private readonly Queue<T> queue = new Queue<T>();
private readonly int maxSize;
public SizeQueue(int maxSize) { this.maxSize = maxSize; }
public void Enqueue(T item)
{
lock (queue)
{
while (queue.Count >= maxSize)
{
Monitor.Wait(queue);
}
queue.Enqueue(item);
if (queue.Count == 1)
{
// wake up any blocked dequeue
Monitor.PulseAll(queue);
}
}
}
public T Dequeue()
{
lock (queue)
{
while (queue.Count == 0)
{
Monitor.Wait(queue);
}
T item = queue.Dequeue();
if (queue.Count == maxSize - 1)
{
// wake up any blocked enqueue
Monitor.PulseAll(queue);
}
return item;
}
}
}