Commit 85280f0 1 parent 7012a08 commit 85280f0 Copy full SHA for 85280f0
File tree 2 files changed +89
-0
lines changed
2 files changed +89
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright (c) 2021, Christopher Friedt
3
+ *
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+
7
+ #include < gtest/gtest.h>
8
+
9
+ #include " isomorphic-strings.cpp"
10
+
11
+ using namespace std ;
12
+
13
+ TEST (IsomorphicStrings, egg_add) {
14
+ EXPECT_EQ (true , Solution ().isIsomorphic (" egg" , " add" ));
15
+ }
16
+
17
+ TEST (IsomorphicStrings, foo_bar) {
18
+ EXPECT_EQ (false , Solution ().isIsomorphic (" foo" , " bar" ));
19
+ }
20
+
21
+ TEST (IsomorphicStrings, paper_title) {
22
+ EXPECT_EQ (true , Solution ().isIsomorphic (" paper" , " title" ));
23
+ }
24
+
25
+ TEST (IsomorphicStrings, badc_bada) {
26
+ EXPECT_EQ (false , Solution ().isIsomorphic (" badc" , " bada" ));
27
+ }
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Copyright (c) 2021, Christopher Friedt
3
+ *
4
+ * SPDX-License-Identifier: MIT
5
+ */
6
+
7
+ // clang-format off
8
+ // name: isomorphic-strings
9
+ // url: https://leetcode.com/problems/isomorphic-strings
10
+ // difficulty: 1
11
+ // clang-format on
12
+
13
+ #include < array>
14
+
15
+ using namespace std ;
16
+
17
+ class Solution {
18
+ public:
19
+ bool isIsomorphic (string s, string t) {
20
+ const size_t N = s.size ();
21
+ const size_t M = t.size ();
22
+
23
+ if (N != M) {
24
+ return false ;
25
+ }
26
+
27
+ array<char , 128 > lut;
28
+ lut.fill (-1 );
29
+ array<char , 128 > rlut;
30
+ rlut.fill (-1 );
31
+
32
+ // i 0 1 2
33
+ // s[i] e g g
34
+ // t[i] a d d
35
+ // lut[s[i]] -1 -1 d
36
+ // rlut[t[i]] -1 -1 g
37
+ //
38
+ // lut[] e:a g:d
39
+ // rlut[] a:e d:g
40
+
41
+ for (size_t i = 0 ; i < N; ++i) {
42
+ if (lut[s[i]] == char (-1 )) {
43
+ if (rlut[t[i]] != char (-1 )) {
44
+ return false ;
45
+ }
46
+ lut[s[i]] = t[i];
47
+ rlut[t[i]] = s[i];
48
+ continue ;
49
+ }
50
+
51
+ if (s[i] != rlut[t[i]]) {
52
+ return false ;
53
+ }
54
+
55
+ if (t[i] != lut[s[i]]) {
56
+ return false ;
57
+ }
58
+ }
59
+
60
+ return true ;
61
+ }
62
+ };
You can’t perform that action at this time.
0 commit comments