|
70 | 70 |
|
71 | 71 | <!-- 这里可写通用的实现逻辑 -->
|
72 | 72 |
|
| 73 | +**方法一:模拟** |
| 74 | + |
| 75 | +我们先遍历 `indices`,对于每个 $i$,如果 `s[indices[i]: indices[i] + len(sources[i])] == sources[i]`,则说明 $s$ 中从 `indices[i]` 开始的 `len(sources[i])` 个字符与 `sources[i]` 相等,我们记录下标 `indices[i]` 处需要替换的是 `targets[i]`,否则不需要替换。 |
| 76 | + |
| 77 | +然后我们从左到右遍历 $s$,如果当前下标 $i$ 处需要替换,则将 `targets[d[i]]` 加入答案,并且 $i$ 跳过 `len(sources[d[i]])` 个字符,否则将 `s[i]` 加入答案,然后 $i$ 自增 $1$。 |
| 78 | + |
| 79 | +时间复杂度 $O(k + n)$,空间复杂度 $O(n)$。其中 $k$ 和 $n$ 分别是 `indices` 和 $s$ 的长度。 |
| 80 | + |
73 | 81 | <!-- tabs:start -->
|
74 | 82 |
|
75 | 83 | ### **Python3**
|
76 | 84 |
|
77 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
78 | 86 |
|
79 | 87 | ```python
|
80 |
| - |
| 88 | +class Solution: |
| 89 | + def findReplaceString(self, s: str, indices: List[int], sources: List[str], targets: List[str]) -> str: |
| 90 | + n = len(s) |
| 91 | + d = [-1] * n |
| 92 | + for i, (j, source) in enumerate(zip(indices, sources)): |
| 93 | + if s[j: j + len(source)] == source: |
| 94 | + d[j] = i |
| 95 | + ans = [] |
| 96 | + i = 0 |
| 97 | + while i < n: |
| 98 | + if d[i] >= 0: |
| 99 | + ans.append(targets[d[i]]) |
| 100 | + i += len(sources[d[i]]) |
| 101 | + else: |
| 102 | + ans.append(s[i]) |
| 103 | + i += 1 |
| 104 | + return ''.join(ans) |
81 | 105 | ```
|
82 | 106 |
|
83 | 107 | ### **Java**
|
84 | 108 |
|
85 | 109 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
86 | 110 |
|
87 | 111 | ```java
|
| 112 | +class Solution { |
| 113 | + public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) { |
| 114 | + int n = s.length(); |
| 115 | + int[] d = new int[n]; |
| 116 | + Arrays.fill(d, -1); |
| 117 | + for (int i = 0; i < indices.length; ++i) { |
| 118 | + int j = indices[i]; |
| 119 | + String source = sources[i]; |
| 120 | + if (s.substring(j, Math.min(n, j + source.length())).equals(source)) { |
| 121 | + d[j] = i; |
| 122 | + } |
| 123 | + } |
| 124 | + StringBuilder ans = new StringBuilder(); |
| 125 | + for (int i = 0; i < n; ) { |
| 126 | + if (d[i] >= 0) { |
| 127 | + ans.append(targets[d[i]]); |
| 128 | + i += sources[d[i]].length(); |
| 129 | + } else { |
| 130 | + ans.append(s.charAt(i++)); |
| 131 | + } |
| 132 | + } |
| 133 | + return ans.toString(); |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +### **C++** |
| 139 | + |
| 140 | +```cpp |
| 141 | +class Solution { |
| 142 | +public: |
| 143 | + string findReplaceString(string s, vector<int>& indices, vector<string>& sources, vector<string>& targets) { |
| 144 | + int n = s.size(); |
| 145 | + vector<int> d(n, -1); |
| 146 | + for (int i = 0; i < indices.size(); ++i) { |
| 147 | + int j = indices[i]; |
| 148 | + string source = sources[i]; |
| 149 | + if (s.substr(j, source.size()) == source) { |
| 150 | + d[j] = i; |
| 151 | + } |
| 152 | + } |
| 153 | + string ans; |
| 154 | + for (int i = 0; i < n;) { |
| 155 | + if (d[i] >= 0) { |
| 156 | + ans += targets[d[i]]; |
| 157 | + i += sources[d[i]].size(); |
| 158 | + } else { |
| 159 | + ans += s[i++]; |
| 160 | + } |
| 161 | + } |
| 162 | + return ans; |
| 163 | + } |
| 164 | +}; |
| 165 | +``` |
88 | 166 |
|
| 167 | +### **Go** |
| 168 | +
|
| 169 | +```go |
| 170 | +func findReplaceString(s string, indices []int, sources []string, targets []string) string { |
| 171 | + n := len(s) |
| 172 | + d := make([]int, n) |
| 173 | + for i, j := range indices { |
| 174 | + source := sources[i] |
| 175 | + if s[j:min(j+len(source), n)] == source { |
| 176 | + d[j] = i + 1 |
| 177 | + } |
| 178 | + } |
| 179 | + ans := &strings.Builder{} |
| 180 | + for i := 0; i < n; { |
| 181 | + if d[i] > 0 { |
| 182 | + ans.WriteString(targets[d[i]-1]) |
| 183 | + i += len(sources[d[i]-1]) |
| 184 | + } else { |
| 185 | + ans.WriteByte(s[i]) |
| 186 | + i++ |
| 187 | + } |
| 188 | + } |
| 189 | + return ans.String() |
| 190 | +} |
| 191 | +
|
| 192 | +func min(a, b int) int { |
| 193 | + if a < b { |
| 194 | + return a |
| 195 | + } |
| 196 | + return b |
| 197 | +} |
89 | 198 | ```
|
90 | 199 |
|
91 | 200 | ### **...**
|
|
0 commit comments