File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments