陣列旋轉 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