用pyqt写出:按下无重复子串”按钮后,在左侧文本框输出字符串“abcbaccabqwer”的所有无重复连续子串,每行显示一个子串,参考下图。同时,按下“最大长度”按钮后,在其右侧小文本框输出最长子串的长度。参考方法:①创建一个数组;②从第一个字符开始遍历,不重复的字符就将它放到新的数组中,遇到重复的就停止。
时间: 2024-04-15 13:26:23 浏览: 80
Python3 无重复字符的最长子串的实现
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QTextEdit, QPushButton
class SubstringWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Substring Window")
self.setGeometry(100, 100, 400, 300)
self.input_label = QLabel("Input String:")
self.input_lineedit = QLineEdit()
self.output_label = QLabel("Substring List:")
self.output_textedit = QTextEdit()
self.length_button = QPushButton("Max Length")
self.substring_button = QPushButton("Substrings")
self.length_button.clicked.connect(self.calculate_max_length)
self.substring_button.clicked.connect(self.calculate_substrings)
layout = QVBoxLayout()
layout.addWidget(self.input_label)
layout.addWidget(self.input_lineedit)
layout.addWidget(self.output_label)
layout.addWidget(self.output_textedit)
layout.addWidget(self.length_button)
layout.addWidget(self.substring_button)
self.setLayout(layout)
def calculate_max_length(self):
input_string = self.input_lineedit.text()
max_length = 0
for i in range(len(input_string)):
substring = []
for j in range(i, len(input_string)):
if input_string[j] not in substring:
substring.append(input_string[j])
else:
break
max_length = max(max_length, len(substring))
self.output_textedit.setText(str(max_length))
def calculate_substrings(self):
input_string = self.input_lineedit.text()
substrings = []
for i in range(len(input_string)):
substring = ""
for j in range(i, len(input_string)):
if input_string[j] not in substring:
substring += input_string[j]
else:
break
substrings.append(substring)
self.output_textedit.setText("\n".join(substrings))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = SubstringWindow()
window.show()
sys.exit(app.exec_())
阅读全文