Skip to content

Commit 83b877c

Browse files
committed
Image Overlap
1 parent f93b813 commit 83b877c

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

Array/835. Image Overlap/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Time spent : 5 min(See hint)
2+
3+
GIVEN: Two images A and B, represented as binary, square matrices of the same size
4+
5+
RETURNS: the largest possible overlap
6+
7+
EXAMPLES:
8+
9+
**Example 1:**
10+
11+
```
12+
Input: A = [[1,1,0],
13+
[0,1,0],
14+
[0,1,0]]
15+
B = [[0,0,0],
16+
[0,1,1],
17+
[0,0,1]]
18+
Output: 3
19+
Explanation: We slide A to right by 1 unit and down by 1 unit.
20+
```
21+
22+
Algorithm:
23+
24+
We can see from the example that in A and B, A\[0][0] and B\[1][1] can be at the same position, A\[0][0] and B\[1][2] can be at the same position, so what we can count is the delta between x and y of same 1 in A and B. So we need to record the delta, if we say delta is 3, then their are 3 unit that are overlap.
25+
26+
e.g. In the example, A\[0][0] and B\[1][1], A\[0][1] and B\[1][2], A\[1][1] and B\[2][2].
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public int largestOverlap(int[][] A, int[][] B) {
3+
int N = A.length;
4+
int[][] count = new int[2 * N + 1][2 * N + 1];
5+
6+
for (int i = 0; i < N; i++) {
7+
for (int j = 0; j < N; j++) {
8+
if (A[i][j] == 1) {
9+
for (int k = 0; k < N; k++) {
10+
for (int l = 0; l < N; l++) {
11+
if (B[k][l] == 1) {
12+
count[k - i + N][l - j + N] += 1;
13+
}
14+
}
15+
}
16+
}
17+
}
18+
}
19+
20+
int ans = 0;
21+
for (int[] val : count) {
22+
for (int y : val) {
23+
ans = Math.max(ans, y);
24+
}
25+
}
26+
return ans;
27+
}
28+
}

HashTable/205. Isomorphic Strings/Solution.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,32 @@ public boolean isIsomorphic(String s, String t) {
1414
}
1515
return true;
1616
}
17+
}
18+
19+
class Solution {
20+
public boolean isIsomorphic(String s, String t) {
21+
if (s.length() != t.length()) {
22+
return false;
23+
}
24+
int len = s.length();
25+
Map<Character, Character> sToT = new HashMap<>();
26+
Map<Character, Character> tToS = new HashMap<>();
27+
for (int i = 0; i < len; i++) {
28+
char a = s.charAt(i);
29+
char b = t.charAt(i);
30+
if (!sToT.containsKey(a) && !tToS.containsKey(b)) {
31+
sToT.put(a, b);
32+
tToS.put(b, a);
33+
}
34+
else {
35+
if (!sToT.containsKey(a) || !tToS.containsKey(b)) {
36+
return false;
37+
}
38+
else if (sToT.get(a) != b || tToS.get(b) != a) {
39+
return false;
40+
}
41+
}
42+
}
43+
return true;
44+
}
1745
}

0 commit comments

Comments
 (0)