leetcode链接:833. 字符串中的查找与替换
题目分析




方案一
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
| class Solution { public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) { StringBuilder result = new StringBuilder(s); int changeIndex = 0; boolean flag = false; int[][] indicesIndex = new int[indices.length][2]; for (int i = 0; i < indices.length; i++) { indicesIndex[i][0] = indices[i]; indicesIndex[i][1] = i; } Arrays.sort(indicesIndex, (o1, o2) -> o1[0] - o2[0]); for (int i = 0; i < indicesIndex.length; i++) { while (i < indicesIndex.length - 1 && indicesIndex[i][0] + sources[indicesIndex[i][1]].length() > indicesIndex[i + 1][0]) { i++; flag = true; } if (false) { i++; flag = false; continue; } if (result.substring(changeIndex + indicesIndex[i][0], changeIndex + indicesIndex[i][0] + sources[indicesIndex[i][1]].length()).equals(sources[indicesIndex[i][1]])) { result.delete(changeIndex + indicesIndex[i][0], changeIndex + indicesIndex[i][0] + sources[indicesIndex[i][1]].length()); result.insert(changeIndex + indicesIndex[i][0], targets[indicesIndex[i][1]]); changeIndex += targets[indicesIndex[i][1]].length() - sources[indicesIndex[i][1]].length(); } } return result.toString(); } }
|
结果
解答成功:
执行耗时:2 ms,击败了69.90% 的Java用户
内存消耗:40.3 MB,击败了66.99% 的Java用户
分析
时间复杂度:
O( n logn )
空间复杂度:
O( n )
官方题解
https://leetcode.cn/problems/find-and-replace-in-string/solutions/2387388/zi-fu-chuan-zhong-de-cha-zhao-yu-ti-huan-9ns4/
方法一:按照下标排序 + 模拟


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
| class Solution { public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) { int n = s.length(), m = indices.length;
List<Integer> ops = new ArrayList<>(); for (int i = 0; i < m; i++) { ops.add(i); } ops.sort((i, j) -> indices[i] - indices[j]);
StringBuilder ans = new StringBuilder(); int pt = 0; for (int i = 0; i < n;) { while (pt < m && indices[ops.get(pt)] < i) { pt++; } boolean succeed = false; while (pt < m && indices[ops.get(pt)] == i) { if (s.substring(i, Math.min(i + sources[ops.get(pt)].length(), n)).equals(sources[ops.get(pt)])) { succeed = true; break; } pt++; } if (succeed) { ans.append(targets[ops.get(pt)]); i += sources[ops.get(pt)].length(); } else { ans.append(s.charAt(i)); i++; } } return ans.toString(); } }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|

方法二:哈希表 + 模拟

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
| class Solution { public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) { int n = s.length(), m = indices.length;
Map<Integer, List<Integer>> ops = new HashMap<Integer, List<Integer>>(); for (int i = 0; i < m; ++i) { ops.putIfAbsent(indices[i], new ArrayList<Integer>()); ops.get(indices[i]).add(i); }
StringBuilder ans = new StringBuilder(); for (int i = 0; i < n;) { boolean succeed = false; if (ops.containsKey(i)) { for (int pt : ops.get(i)) { if (s.substring(i, Math.min(i + sources[pt].length(), n)).equals(sources[pt])) { succeed = true; ans.append(targets[pt]); i += sources[pt].length(); break; } } } if (!succeed) { ans.append(s.charAt(i)); ++i; } } return ans.toString(); } }
作者:力扣官方题解 链接:https: 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
|
