A very underappreciated gem in the standard lib is difflib
...
>>>
import difflib
>>>
s = difflib.SequenceMatcher(None, "GHSKWITNIFSI", "GHSKWAGDITNIFSI") >>>
s.get_matching_blocks()[: -1]
[(0, 0, 5), (5, 8, 7)] >>>
s = difflib.SequenceMatcher(None, "GHSKWITNIFSI", "GHSKWITNIFSI") >>>
s.get_matching_blocks()[: -1]
[(0, 0, 12)]
This ... feels kludgy to a degree, and it's only probably half-way there, but it seems like it found the substring in your example and could probably be expanded a bit. I can revise it some in a minute with some more time to test, but it's an approach concept:
s1 = 'GHSKWITNIFSI'
s2 = 'GHSKWAGDITNIFSI'
l = len(s2) - len(s1)
for i in range(len(s1)):
if s2[0: i] + s2[i + l: ] == s1:
print i
break
def GetInsertedString(StringA, StringB):
lenA = len(StringA)
lenB = len(StringB)
if lenA > lenB:
return None, None
begincount = 0
while begincount < lenA and StringA[begincount] == StringB[begincount]:
begincount += 1
endcount = 0
while endcount < (lenA - begincount) and StringA[lenA - endcount - 1] == StringB[lenB - endcount - 1]:
endcount += 1
if begincount + endcount != lenA:
return None, None
return begincount, StringB[begincount: begincount + lenB - lenA]
>>>
GetInsertedString('GHSKWITNIFSI', 'GHSKWAGDITNIFSI')
(5, 'AGD') >>>
GetInsertedString('GHSKWITNIFSI', 'GHSKWAGDTNIFSI')
(None, None)
from itertools
import dropwhile
def get_inserted_substring(s1, s2):
try:
# diff is the first index at which the strings differ
diff = dropwhile(lambda i: s1[i] == s2[i], xrange(len(s2))).next()
if s2[diff: ].endswith(s1[diff: ]):
return (diff, s2[diff: diff - len(s1)])
except(StopIteration, IndexError):
# the strings are the same or only differ at the end
if len(s1) <= len(s2):
return (len(s1), s2[len(s1): ])
return (None, None)
And examples...
>>> get_inserted_substring('abcdef', 'abcXYZdef')
(3, 'XYZ') >>>
get_inserted_substring('abcdef', 'abRSTcdXYZef')
(None, None) >>>
get_inserted_substring('abcdef', 'abcdefXYZ')
(6, 'XYZ') >>>
get_inserted_substring('abcdef', 'XYZabcdef')
(0, 'XYZ') >>>
get_inserted_substring('abcdefXYZ', 'abcdef')
(None, None)
strA = 'foor'
strB = 'foobar'
strC = 'ba'
if strB.replace(strC, '') == strA:
print strC, ' at index ', len(strB.split(strC)[0])
Last Updated : 17 Jul, 2022
Example 1:
Input:
str1 = "heap", str2 = "pea"
Output:
Minimum Deletion = 2 and
Minimum Insertion = 1
Explanation:
p and h deleted from heap
Then, p is inserted at the beginning
One thing to note, though p was required yet
it was removed / deleted first from its position and
then it is inserted to some other position.
Thus, p contributes one to the deletion_count
and one to the insertion_count.
Example 2:
Input:
str1 = "geeksforgeeks", str2 = "geeks"
Output:
Minimum Deletion = 8
Minimum Insertion = 0
Minimum number of deletions = 2 Minimum number of insertions = 1
Minimum number of deletions = 2 Minimum number of insertions = 1
non-member overloads: getline (string) operator+ (string) operator<< (string) operator>> (string) relational operators (string) swap (string) ,Other: <algorithm> <bitset> <chrono> <codecvt> <complex> <exception> <functional> <initializer_list> <iterator> <limits> <locale> <memory> <new> <numeric> <random> <ratio> <regex> <stdexcept> <string> <system_error> <tuple> <typeindex> <typeinfo> <type_traits> <utility> <valarray> ,functions: stod stof stoi stol stold stoll stoul stoull to_string to_wstring ,classes: string u16string u32string wstring
string & insert(size_t pos,
const string & str);
string & insert(size_t pos,
const string & str, size_t subpos, size_t sublen);
string & insert(size_t pos,
const char * s);
string & insert(size_t pos,
const char * s, size_t n);
string & insert(size_t pos, size_t n, char c);
void insert(iterator p, size_t n, char c);
iterator insert(iterator p, char c);
What's the best way of checking if StringA = StringB with an another StringC inserted at some arbitrary point? , 1 week ago Feb 07, 2019 · size_t find (const string& str, size_t pos = 0); size_t find (const char* s, size_t pos = 0); Function parameters: str : The sub-string to be searched. s : The sub-string to be searched, given as C style string. pos : The initial position from where the string search is to begin. Function Return: , 5 days ago What we can do regarding the string insertion is to create a new string with the desired changes, like splitting the original string and inserting a new string in it. Insert String Into a String in Python Using the string.find() Method. We first use the string.find() method to get the substring index in the string, after which we need to insert ... , 4 days ago Oct 27, 2014 · CHARINDEX can start at a certain position in the string while PATINDEX can take wildcards. In this simplistic case, we can use either one. I …
>>>
import difflib >>> s = difflib.SequenceMatcher(None, "GHSKWITNIFSI", "GHSKWAGDITNIFSI") >>> s.get_matching_blocks()[: -1][(0, 0, 5), (5, 8, 7)] >>> s = difflib.SequenceMatcher(None, "GHSKWITNIFSI", "GHSKWITNIFSI") >>> s.get_matching_blocks()[: -1][(0, 0, 12)]
Generated by cloudfront(CloudFront) Request ID: v - 0 emDsM57Dh - i5feh9B3uM5jAXusH_KImNRR2z8aQoimFCxjBHYxQ ==