Repository navigation
Expand file tree
/
Copy pathsolution.java
More file actions
79 lines (63 loc) · 2.12 KB
/
Copy pathsolution.java
File metadata and controls
79 lines (63 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Solution {
public String generateString(String str1, String str2) {
int n = str1.length();
int m = str2.length();
int len = n + m - 1;
char[] ans = new char[len];
boolean[] fixed = new boolean[len];
for (int i = 0; i < len; i++) {
ans[i] = '?';
}
// Apply all 'T' constraints
for (int i = 0; i < n; i++) {
if (str1.charAt(i) == 'T') {
for (int j = 0; j < m; j++) {
int pos = i + j;
char ch = str2.charAt(j);
if (ans[pos] != '?' && ans[pos] != ch) {
return "";
}
ans[pos] = ch;
fixed[pos] = true;
}
}
}
// Fill remaining positions with 'a'
for (int i = 0; i < len; i++) {
if (ans[i] == '?')
ans[i] = 'a';
}
// Process all 'F' constraints
for (int i = 0; i < n; i++) {
if (str1.charAt(i) == 'F') {
boolean same = true;
for (int j = 0; j < m; j++) {
if (ans[i + j] != str2.charAt(j)) {
same = false;
break;
}
}
if (!same)
continue;
boolean changed = false;
for (int j = m - 1; j >= 0; j--) {
int pos = i + j;
if (fixed[pos])
continue;
for (char c = 'a'; c <= 'z'; c++) {
if (c != ans[pos] && c != str2.charAt(j)) {
ans[pos] = c;
changed = true;
break;
}
}
if (changed)
break;
}
if (!changed)
return "";
}
}
return new String(ans);
}
}