Các phương thức chuỗi python phức tạp về thời gian
Cho hai xâu s1 và s2, tìm xem s1 có phải là xâu con của s2 không. Nếu có, trả về chỉ mục của lần xuất hiện đầu tiên, nếu không trả về -1 Show ví dụ. Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output: -1. Explanation: There is no occurrence of "practice" in "geeksforgeeks" Khuyến khích. Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp Cách tiếp cận đơn giản. Ý tưởng là chạy một vòng lặp từ đầu đến cuối và với mọi chỉ mục trong chuỗi đã cho, hãy kiểm tra xem chuỗi con có thể được tạo từ chỉ mục đó không. Điều này có thể được thực hiện bằng cách chạy một vòng lặp lồng nhau duyệt qua chuỗi đã cho và trong vòng lặp đó chạy một vòng lặp khác để kiểm tra chuỗi con từ mọi chỉ mục. Python3
_______ 132 _______ _______ 133 _______ Present at index 50 Present at index 51_______1_______2 Present at index 53 Present at index 54 Present at index 50 Present at index 56 Present at index 52 Present at index 53 Present at index 59
Present at index 50 182 Present at index 50 184 185 186 187 188 189 Present at index 51 # Python3 program to check if 1 # Python3 program to check if 2# Python3 program to check if 3
184 # a string is substring of other. 1186 187 # a string is substring of other. 4
Present at index 52 1
Present at index 52 Present at index 52 # Returns true if s1 is substring 2
185
Present at index 50 # Returns true if s1 is substring 4 189 # Python3 program to check if 2
Present at index 52 Present at index 52 # of s2 7# of s2 8Present at index 50 def 0Present at index 52 def 2Present at index 50 def 4Present at index 52 def 6Present at index 50 def 8Present at index 52 isSubstring(s1, s2): 0Present at index 50 # a string is substring of other. 6 def 8Present at index 52 Present at index 52 189 # Python3 program to check if 2 # of s2 8
Present at index 500 Present at index 501 Present at index 502 Present at index 503 Present at index 50 Present at index 505 # of s2 8
Present at index 500 Present at index 501 Present at index 510 # Python3 program to check if 1 Present at index 512 Present at index 513 Present at index 514 đầu ra Present at index 5 Phân tích độ phức tạp.
Một giải pháp hiệu quả là sử dụng thuật toán tìm kiếm O(n) như thuật toán KMP, thuật toán Z, v.v.
Một giải pháp hiệu quả khác.
Python3Present at index 515
Present at index 517 Present at index 518 Present at index 519 Present at index 520 Present at index 50 Present at index 522_______1_______2 Present at index 524 Present at index 50 Present at index 526 Present at index 52 Present at index 53 Present at index 501 Present at index 518 Present at index 503 Present at index 50 185 Present at index 52 Present at index 524 Present at index 520 Present at index 50 Present at index 538 Present at index 50 184 185 186 187 Present at index 501 Present at index 526 # Python3 program to check if 3
Present at index 549 Present at index 52 Present at index 52 Present at index 53 Present at index 553
Present at index 501 Present at index 518 Present at index 560 Present at index 52 Present at index 52 Present at index 563
Present at index 522 # Python3 program to check if 1Present at index 52 # Python3 program to check if 2
Present at index 505 # of s2 8
Present at index 522 Present at index 52 Present at index 524
Present at index 50 # a string is substring of other. 6 Present at index 579 Present at index 53_______1_______53
189 # Python3 program to check if 2Present at index 50 Present at index 505 # of s2 8
Present at index 591 189 Present at index 593
Present at index 595 Present at index 500 Present at index 597 Present at index 598 Present at index 599 1800 1801 Present at index 500 Present at index 597 Present at index 598 Present at index 599 1806 1801
1809 đầu ra 18 Phân tích độ phức tạp Độ phức tạp của đoạn mã trên sẽ vẫn là O(n*m) trong trường hợp xấu nhất và độ phức tạp của không gian là O(1) Độ phức tạp thời gian của các phương thức chuỗi là gì?Độ phức tạp về thời gian của nó là O(N + M) trong đó N là kích thước của chuỗi đầu tiên và M là kích thước của chuỗi thứ hai . sao chép(). Sao chép chuỗi con của chuỗi trong chuỗi được truyền dưới dạng tham số và trả về số ký tự đã sao chép. Độ phức tạp thời gian của nó là O(N) trong đó N là kích thước của chuỗi được sao chép.
Độ phức tạp về thời gian của find() trong Python là gì?Câu trả lời của bạn là O(m*n) cho CPython.
Độ phức tạp về thời gian của việc lập chỉ mục chuỗi Python là gì?Thuật toán đánh đổi không gian lấy thời gian để có được độ phức tạp trường hợp trung bình là O(N) trên văn bản ngẫu nhiên, mặc dù nó có . Hy vọng rằng sẽ giúp. |