This repository was archived by the owner on Sep 8, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +105
-0
lines changed Expand file tree Collapse file tree 2 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * isIsomorphic - check if two strings are isomorphic
3
+ * @s: the string to be checked
4
+ * @t: the second string to be equalized to
5
+ *
6
+ * Return: true if isomorphic, false otherwise
7
+ */
8
+ bool isIsomorphic (char * s , char * t )
9
+ {
10
+ int len = strlen (s ), i ;
11
+ char map [256 ] = {0 };
12
+ char rmap [256 ] = {0 };
13
+
14
+ /**checks if the lengths are the same **/
15
+ if (len != strlen (t ))
16
+ {
17
+ return (false);
18
+ }
19
+
20
+ for (i = 0 ; i < len ; i ++ )
21
+ {
22
+ if (map [s [i ]] == 0 && rmap [t [i ]] == 0 )
23
+ {
24
+ map [s [i ]] = t [i ];
25
+ rmap [t [i ]] = s [i ];
26
+ }
27
+ else if (map [s [i ]] != t [i ] || rmap [t [i ]] != s [i ])
28
+ {
29
+ return (false);
30
+ }
31
+ }
32
+ return (true);
33
+ }
34
+
35
+ /** the best code **/
36
+
37
+ bool isIsomorphic (char * s , char * t )
38
+ {
39
+
40
+ int hash_1 [127 ] = {0 };
41
+ int hash_2 [127 ] = {0 };
42
+ int len = strlen (s ), i = 0 ;
43
+
44
+ while (i < len )
45
+ {
46
+ if (hash_1 [s [i ]] != hash_2 [t [i ]])
47
+ return (false);
48
+
49
+ else
50
+ {
51
+ hash_1 [s [i ]] = i + 1 ;
52
+ hash_2 [t [i ]] = i + 1 ;
53
+ }
54
+ i ++ ;
55
+ }
56
+ return (true);
57
+ }
58
+
59
+ /** something to try **/
60
+ bool isIsomorphic (char * s , char * t )
61
+ {
62
+ int hashS [128 ], hashT [128 ];
63
+ int index = 0 , i ;
64
+
65
+
66
+ for (i = 0 ; i < 128 ; i ++ ) {
67
+ hashS [i ] = -1 ;
68
+ hashT [i ] = -1 ;
69
+ }
70
+ while (s [index ] != '\0' )
71
+ {
72
+ if (hashS [s [index ]] == -1 && hashT [t [index ]] == -1 )
73
+ {
74
+ hashS [s [index ]] = t [index ];
75
+ hashT [t [index ]] = s [index ];
76
+ }
77
+ else if (hashS [s [index ]] != t [index ] || hashT [t [index ]] != s [index ])
78
+ return (false);
79
+ index ++ ;
80
+ }
81
+ return (true);
82
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * lengthOfLastWord - a function to find the length of
3
+ * the last word.
4
+ * @s: string
5
+ *
6
+ * Return: length of last word
7
+ */
8
+ int lengthOfLastWord (char * s )
9
+ {
10
+ int len = strlen (s );
11
+ int i = len - 1 , j ;
12
+
13
+ while (i >= 0 && s [i ] == ' ' )
14
+ {
15
+ i -- ;
16
+ }
17
+ j = i ;
18
+ while (j >= 0 && s [j ] != ' ' )
19
+ {
20
+ j -- ;
21
+ }
22
+ return (i - j );
23
+ }
You can’t perform that action at this time.
0 commit comments