手机 网站 系统,微商软件自助商城,千万别做网络管理员,seo如何优化排名文章目录 Find And Replace in String 字符串中的查找与替换问题描述#xff1a;分析代码排序模拟 Tag Find And Replace in String 字符串中的查找与替换
问题描述#xff1a;
你会得到一个字符串 s (索引从 0 开始)#xff0c;你必须对它执行 k 个替换操作。替换操作以三… 文章目录 Find And Replace in String 字符串中的查找与替换问题描述分析代码排序模拟 Tag Find And Replace in String 字符串中的查找与替换
问题描述
你会得到一个字符串 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 [abbc] 的测试用例将不会生成因为 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] 仅由小写英文字母组成 1s.length1000kindices.lengthsources.lengthtargets.length1k1000indices[i]s.length1sources[i].length,targets[i].length50s仅由小写英文字母组成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] idxind[i] l s s o u r c e [ i ] . l e n g t h ls source[i].length lssource[i].length.即 s [ i d x : i d x l s − 1 ] s o u r c e [ i ] s[idx:idxls-1]source[i] s[idx:idxls−1]source[i]是否相等。
如果相等 s [ i d x : i d x l s − 1 ] s[idx:idxls-1] s[idx:idxls−1]就需要被替换。因为有限制所以不会出现重叠的现象。此时 s [ i d x l s ] s[idxls] s[idxls]就是未被替换的字符位置它和下一个待比较的索引位置的关系一定是 i d x l s n e x t i d x idxlsnextidx idxlsnextidx.
所以在2个待比较的索引之间未被替换的字符串的长度是0,这一部分就需要加入到结果中。
如果不相等那么 s [ i d x : n e x t i d x − 1 ] s[idx:nextidx-1] s[idx:nextidx−1]的字符串就会全部加入结果中。 模拟 这个思路是以待比较的索引从小到大的排列并且以索引数组为遍历目标进行模拟。
因此需要对原始的三个数组进行预处理即进行索引排序。
代码 排序模拟
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 i0;in;i) indarr[i] i;Arrays.sort(indarr,(a,b)-{return indices[a]-indices[b];});int end 0;for(int i0;in;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(endidx){ sb.append(s.substring(end,idx));} sb.append(t1);end idxl1;}else{int nx i1n?indices[indarr[i1]]:len;if(idxnx) continue; sb.append(s.substring(end,idx));sb.append(s.substring(idx,nx));end nx;} } if(endlen)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(l1ls) return false;int ind 0;for(int i idx;iidxls;i,ind){ if(arr[i]!s.charAt(ind)) return false; }return true; }
}时间复杂度 O ( N M l o g M ) O(NMlogM ) O(NMlogM)
空间复杂度 O ( N M L ) O(NML) O(NML)
还有一种线性的思路S的长度是N那么就有可能会在N个位置上进行替换操作。有空再写
Tag
Array
Sort
String