数组旋转 Python 程序
在本文中,我们将了解如下所示问题陈述的解决方案。
问题陈述 − 给定一个文本和一个模式,我们需要打印文本中模式及其排列(或同义词)的所有出现。
现在让我们在下面的实现中观察解决方案 −
示例
# maximum value
MAX = 300
# compare
def compare(arr1, arr2):
for i in range(MAX):
if arr1[i] != arr2[i]:
return False
return True
# search
def search(pat, txt):
M = len(pat)
N = len(txt)
# countP pattern account
# countTW text window count
countP = [0]*MAX
countTW = [0]*MAX
for i in range(M):
(countP[ord(pat[i]) ]) += 1
(countTW[ord(txt[i]) ]) += 1
# Traversal
for i in range(M, N):
# Compare current window and patten counts
if compare(countP, countTW):
print("Found at Index", (i-M))
# Add charcter to window
(countTW[ ord(txt[i]) ]) += 1
# remove charcter from window
(countTW[ ord(txt[i-M]) ]) -= 1
# Check for the last window
if compare(countP, countTW):
print("It is Found at Index : ", N-M)
# main
txt = "TUTORIALSPOINT"
pat = "TOR"
search(pat, txt)输出
Found at Index 2

所有变量均在局部作用域中声明,并且在上图中可以看到它们的引用。
结论
在本文中,我们了解了如何编写同义词子字符串搜索的 Python 程序。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP