1
+ class Solution :
2
+ def findReplaceString (self , s : str , indices : List [int ], sources : List [str ], targets : List [str ]) -> str :
3
+ """
4
+
5
+ if we replace then at that time the string is not updated
6
+
7
+ run the loop till k
8
+ i in range k
9
+
10
+ start matching sources[i] string with s[i:]
11
+ if any character is not equal
12
+ then just the current string cannot be replaced
13
+ and continue with the next i
14
+
15
+ if all match then store it in array of output strings
16
+
17
+ indices are not sorted
18
+ []
19
+
20
+ how will we find the remaining string which is not replaced
21
+
22
+ "-b--"
23
+ {
24
+ 0: "eee"
25
+ 2: "ffff"
26
+ }
27
+
28
+ "eeebffff"
29
+
30
+ "vbfrssozp"
31
+ """
32
+
33
+ def matchStringSameLen (str1 , str2 ):
34
+ for i in range (len (str1 )):
35
+ if str1 [i ] != str2 [i ]:
36
+ return False
37
+ return True
38
+
39
+
40
+ i = 0
41
+
42
+ startIndexToReplaceStringMap = {}
43
+ outputString = ""
44
+ inputStringIntoList = list (s )
45
+
46
+ k = len (indices )
47
+ while i < k :
48
+ substringLen = len (sources [i ])
49
+ startIndex = indices [i ]
50
+
51
+ isMatch = matchStringSameLen (s [startIndex :startIndex + substringLen ], sources [i ])
52
+
53
+ if isMatch :
54
+ startIndexToReplaceStringMap [startIndex ] = (targets [i ], substringLen )
55
+
56
+ # replace substring in original string by placeholder value
57
+ # print(startIndex)
58
+ # print(substringLen)
59
+ for j in range (startIndex , startIndex + substringLen ):
60
+ inputStringIntoList [j ] = "-"
61
+
62
+ i += 1
63
+
64
+ # print(inputStringIntoList)
65
+ # print(startIndexToReplaceStringMap)
66
+
67
+ index = 0
68
+ while index < len (inputStringIntoList ):
69
+ # print(index)
70
+ if inputStringIntoList [index ] != '-' :
71
+ outputString += inputStringIntoList [index ]
72
+ index += 1
73
+ else :
74
+ # if s[index] == '-' -> copy the contents from hashmap with the same start index and do index += until all - are over
75
+
76
+ outputString += startIndexToReplaceStringMap [index ][0 ]
77
+ # we need to skip '-' until length of the original word
78
+ subStrLen = startIndexToReplaceStringMap [index ][1 ]
79
+ # print("subStrLen: ", subStrLen)
80
+ index += subStrLen
81
+
82
+ return outputString
83
+
84
+
85
+
86
+
87
+
88
+
89
+
0 commit comments