Skip to content

Commit e41ff05

Browse files
committed
dictionary application
1 parent 9276135 commit e41ff05

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace HashTable.Lib
7+
{
8+
public class IsomorphicStringsSln
9+
{
10+
public bool IsIsomorphic(string s, string t)
11+
{
12+
var dict = new Dictionary<char, char>(); //s.char->t.char
13+
var dict2 = new Dictionary<char, char>(); //t.char->s.char
14+
for (int i = 0; i < s.Length; i++)
15+
{
16+
if (dict.ContainsKey(s[i]) && dict[s[i]] != t[i] ||
17+
dict2.ContainsKey(t[i]) && dict2[t[i]] != s[i])
18+
return false;
19+
dict[s[i]] = t[i];
20+
dict2[t[i]] = s[i];
21+
}
22+
return true;
23+
}
24+
}
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace HashTable.Lib
7+
{
8+
public class WordPattenSln
9+
{
10+
//pattern = "abba", str = "dog cat cat dog"
11+
public bool WordPattern(string pattern, string str){
12+
string[] words = str.Split(' ');
13+
if (pattern.Length != words.Length) return false;
14+
Dictionary<char, string> patternDict = new Dictionary<char, string>();
15+
Dictionary<string, char> wordsDict = new Dictionary<string, char>();
16+
for (int i = 0; i < pattern.Length; i++){
17+
if(patternDict.ContainsKey(pattern[i]) && patternDict[pattern[i]]!=words[i] ||
18+
wordsDict.ContainsKey(words[i]) && wordsDict[words[i]]!=pattern[i] )
19+
return false;
20+
patternDict[pattern[i]] = words[i];
21+
wordsDict[words[i]] = pattern[i];
22+
}
23+
return true;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)