赞
踩
你会得到一个字符串 s
(索引从 0 开始),你必须对它执行 k
个替换操作。替换操作以三个长度均为 k
的并行数组给出:indices
, sources
, targets
。
要完成第 i
个替换操作:
sources[i]
是否出现在 原字符串 s
的索引 indices[i]
处。targets[i]
替换 该子字符串。例如,如果 s = "abcd"
, indices[i] = 0
, sources[i] = "ab"
, targets[i] = "eee"
,那么替换的结果将是 "eeecd"
。
所有替换操作必须 同时 发生,这意味着替换操作不应该影响彼此的索引。测试用例保证元素间不会重叠 。
s = "abc"
, indices = [0,1]
, sources = ["ab","bc"]
的测试用例将不会生成,因为 "ab"
和 "bc"
替换重叠。在对 s
执行所有替换操作后返回 结果字符串 。
子字符串 是字符串中连续的字符序列。
1 < = s . l e n g t h < = 1000 k = = i n d i c e s . l e n g t h = = s o u r c e s . l e n g t h = = t a r g e t s . l e n g t h 1 < = k < = 100 0 < = i n d i c e s [ i ] < s . l e n g t h 1 < = s o u r c e s [ i ] . l e n g t h , t a r g e t s [ i ] . l e n g t h < = 50 s 仅由小写英文字母组成 s o u r c e s [ i ] 和 t a r g e t s [ i ] 仅由小写英文字母组成 1 <= s.length <= 1000\\ k == indices.length == sources.length == targets.length\\ 1 <= k <= 100\\ 0 <= indices[i] < s.length\\ 1 <= sources[i].length, targets[i].length <= 50\\ s 仅由小写英文字母组成\\ sources[i] 和 targets[i] 仅由小写英文字母组成 1<=s.length<=1000k==indices.length==sources.length==targets.length1<=k<=1000<=indices[i]<s.length1<=sources[i].length,targets[i].length<=50s仅由小写英文字母组成sources[i]和targets[i]仅由小写英文字母组成
模拟类型的问题
直接的想法就是依次遍历,遇到一个索引,进行比较,如果于 s o u r c e [ i ] source[i] source[i]相同,就要替换成为 t [ i ] t[i] t[i].
当遇到索引 i d x = i n d [ i ] idx= ind[i] idx=ind[i], l s = s o u r c e [ i ] . l e n g t h ls = source[i].length ls=source[i].length.即 s [ i d x : i d x + l s − 1 ] = = s o u r c e [ i ] s[idx:idx+ls-1]==source[i] s[idx:idx+ls−1]==source[i]是否相等。
所以在2个待比较的索引之间未被替换的字符串的长度是>=0,这一部分就需要加入到结果中。
模拟
这个思路是以待比较的索引从小到大的排列,并且以索引数组为遍历目标,进行模拟。
因此需要对原始的三个数组,进行预处理,即进行索引排序。
class Solution { public String findReplaceString(String s, int[] indices, String[] sources, String[] targets) { StringBuilder sb = new StringBuilder(); char[] arr = s.toCharArray(); int n = indices.length; int len = arr.length; Integer[] indarr = new Integer[n]; for(int i=0;i<n;i++) indarr[i] =i; Arrays.sort(indarr,(a,b)->{return indices[a]-indices[b];}); int end = 0; for(int i=0;i<n;i++){ int idx = indices[indarr[i]]; String s1 = sources[indarr[i]]; String t1 = targets[indarr[i]]; int l1 = s1.length(); if(check(idx,arr,s1)){ if(end<idx){ sb.append(s.substring(end,idx)); } sb.append(t1); end = idx+l1; } else{ int nx = i+1<n?indices[indarr[i+1]]:len; if(idx==nx) continue; sb.append(s.substring(end,idx)); sb.append(s.substring(idx,nx)); end = nx; } } if(end<len)sb.append(s.substring(end,len)); return sb.toString(); } public boolean check(int idx,char[] arr,String s){ int ls = s.length(),n = arr.length; int l1 = n-idx; if(l1<ls) return false; int ind =0; for(int i = idx;i<idx+ls;i++,ind++){ if(arr[i]!=s.charAt(ind)) return false; } return true; } }
时间复杂度 O ( N + M l o g M ) O(N+MlogM ) O(N+MlogM)
空间复杂度 O ( N + M L ) O(N+ML) O(N+ML)
还有一种线性的思路,S的长度是N,那么就有可能会在N个位置上进行替换操作。有空再写
Array
Sort
String
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。