我正在解决一个问题,我需要合并两个字符串,以便一个字符串可以在另一个字符串中。合并的字符串的长度应最短。
示例:
str1 = "AABAK"
str2 = "HYUAABA"
merged string = "HYUAABAK"
到目前为止,我已经能够使它以类似的子字符串结尾的字符串工作,但是如果相反,那么我的解决方案将无法正常工作。
失败:
str1 = 'ctaagt'
str2 = 'gcta'
expected answer: gctaagt
解决方案:
def overlap(str1, str2):
l = min(len(str1), len(str2))
for i in range(l, -1, -1):
if str1.endswith(str2[-i:]):
print('overlap ' + str2[-i:])
return str2[-i:]
str1 = "AABAK"
str2 = "HYUAABA"
for i in range(len(str1), -1, -1):
res = overlap(str1[0:i], str2)
if(res):
print('merge ' + str2+str1[i:])
break
我也想知道是否有更好或更清洁的方法来解决这个问题。
注意:出于测试目的,str1故意短了。
答案 0 :(得分:1)
您可以修改Collecting pandas
Downloading https://files.pythonhosted.org/packages/86/ad/89670f4017b2459dfb5577775efbc4c6c20eb46728ac6e5b721602493724/pandas-0.23.4-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (15.0MB)
100% |████████████████████████████████| 15.0MB 1.5MB/s
Requirement already satisfied: pytz>=2011k in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from pandas) (2013.7)
Collecting python-dateutil>=2.5.0 (from pandas)
Downloading https://files.pythonhosted.org/packages/74/68/d87d9b36af36f44254a8d512cbfc48369103a3b9e474be9bdfe536abfc45/python_dateutil-2.7.5-py2.py3-none-any.whl (225kB)
100% |████████████████████████████████| 235kB 20.5MB/s
Requirement already satisfied: numpy>=1.9.0 in /Library/Python/2.7/site-packages (from pandas) (1.15.4)
Requirement already satisfied: six>=1.5 in /Library/Python/2.7/site-packages/six-1.11.0-py2.7.egg (from python-dateutil>=2.5.0->pandas) (1.11.0)
matplotlib 1.3.1 requires nose, which is not installed.
matplotlib 1.3.1 requires tornado, which is not installed.
Installing collected packages: python-dateutil, pandas
Found existing installation: python-dateutil 1.5
Uninstalling python-dateutil-1.5:
Could not install packages due to an EnvironmentError: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/easter.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/parser.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/relativedelta.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/rrule.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tz.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/tzwin.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.py', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.py', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.pyc', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/zoneinfo-2010g.tar.gz', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/zoneinfo-2010g.tar.gz', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo/zoneinfo-2010g.tar.gz'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil/zoneinfo'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil', '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil', "[Errno 1] Operation not permitted: '/private/var/folders/1_/qlfmn38s0g5gjh76dm_dx6ch0000gn/T/pip-uninstall-a1loqq/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/dateutil'")]
以返回最终的重叠字符串。然后,可以考虑overlap
和str1
的可能排列,并选择str2
为字符串长度的min
的最终结果:
key
答案 1 :(得分:0)
使用tobias_k's solution from a previous post作为参考,您可以执行以下操作。然后只需将a,b和b,a的输出进行比较,看看哪个结果字符串更短。
from functools import reduce
a = "AABAK"
b = "HYUAABA"
def overlap(a, b):
return max(i for i in range(len(b)) if b[i - 1] == a[-1] and a.endswith(b[:i]))
reduce(lambda a, b: a + b[overlap(a, b):], [b, a])
答案 2 :(得分:0)
您可以很简单地计算重叠并在相同的循环中进行字符串合并:
def merge(str1, str2):
str2_len = len(str2)
for i in range(str2_len):
# edited to only match correctly
if str1.startswith(str2[i:]):
return str2 + str1[str2_len-i:]
return str2 + str1
>>> str1 =“ AABAK”
>>> str2 =“ HYUAABA”
>>> merge(str1,str2)
'HYUAABAK'
>>> str1 ='ctaagt'
>>> str2 ='gcta'
>>> merge(str1,str2)
'gctaagt'