File tree 2 files changed +63
-0
lines changed
main/java/com/thealgorithms/strings
test/java/com/thealgorithms/strings 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .strings ;
2
+ import java .util .*;
3
+
4
+ public class Isomorphic {
5
+ public static boolean checkStrings (String s , String t ) {
6
+ if (s .length () != t .length ()){
7
+ return false ;
8
+ }
9
+
10
+ // To mark the characters of string using MAP
11
+ // character of first string as KEY and another as VALUE
12
+ // now check occurence by keeping the track with SET data structure
13
+ Map <Character , Character > characterMap = new HashMap <Character , Character >();
14
+ Set <Character > trackUinqueCharacter = new HashSet <Character >();
15
+
16
+ for (int i =0 ; i <s .length (); i ++){
17
+ if (characterMap .containsKey (s .charAt (i ))){
18
+ if (t .charAt (i ) != characterMap .get (s .charAt (i ))){
19
+ return false ;
20
+ }
21
+ }
22
+ else {
23
+ if (trackUinqueCharacter .contains (t .charAt (i ))){
24
+ return false ;
25
+ }
26
+
27
+ characterMap .put (s .charAt (i ), t .charAt (i ));
28
+ }
29
+ trackUinqueCharacter .add (t .charAt (i ));
30
+ }
31
+ return true ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ package com .thealgorithms .strings ;
2
+ import org .junit .jupiter .api .Test ;
3
+ import static org .junit .jupiter .api .Assertions .*;
4
+ import java .util .*;
5
+
6
+ public class IsomorphicTest {
7
+
8
+ @ Test
9
+ public static void main (String [] args ) {
10
+
11
+ String str1 = "abbbbaac" ;
12
+ String str2 = "kffffkkd" ;
13
+
14
+ String str3 = "xyxyxy" ;
15
+ String str4 = "bnbnbn" ;
16
+
17
+ String str5 = "ghjknnmm" ;
18
+ String str6 = "wertpopo" ;
19
+
20
+ String str7 = "aaammmnnn" ;
21
+ String str8 = "ggghhhbbj" ;
22
+
23
+ Isomorphic isomorphic = new Isomorphic ();
24
+
25
+ assertTrue (isomorphic .checkStrings (str1 , str2 ));
26
+ assertTrue (isomorphic .checkStrings (str3 , str4 ));
27
+ assertFalse (isomorphic .checkStrings (str5 , str6 ));
28
+ assertFalse (isomorphic .checkStrings (str7 , str8 ));
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments