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

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.  
Ví dụ, xét có một xâu có độ dài N và một xâu con có độ dài M. Sau đó chạy một vòng lặp lồng nhau, trong đó vòng lặp bên ngoài chạy từ 0 đến (N-M) và vòng lặp bên trong từ 0 đến M. Đối với rất chỉ mục, hãy kiểm tra xem chuỗi con đi qua vòng lặp bên trong có phải là chuỗi con đã cho hay không.  

Python3




# Python3 program to check if 

# a string is substring of other.

 

# Returns true if s1 is substring 

# of s2

_______ 132 _______ _______ 133 _______

Present at index 5
0
Present at index 5
1_______1_______2
Present at index 5
3
Present at index 5
4

Present at index 5
0
Present at index 5
6
Present at index 5
2
Present at index 5
3
Present at index 5
9

 

Present at index 5
0
18
2

Present at index 5
0
18
4
18
5
18
6
18
7
18
8
18
9
Present at index 5
1# Python3 program to check if 1 # Python3 program to check if 2# Python3 program to check if 3

 

# Python3 program to check if 5# Python3 program to check if 6

# Python3 program to check if 5# Python3 program to check if 8

# Python3 program to check if 5

18
4 # a string is substring of other.1
18
6
18
7# a string is substring of other.4

# a string is substring of other.5# a string is substring of other.6 # a string is substring of other.7# Python3 program to check if 1 # a string is substring of other.9

Present at index 5
2  1

 2____129_______3

 4

# Python3 program to check if 5# a string is substring of other.6 # a string is substring of other.1# Python3 program to check if 1 # Python3 program to check if 2

Present at index 5
2
Present at index 5
2 # Returns true if s1 is substring 2

# a string is substring of other.5# Returns true if s1 is substring 4

18
5

 

Present at index 5
0# Returns true if s1 is substring 4
18
9# Python3 program to check if 2

 

# of s22

# a string is substring of other.6 # of s24

Present at index 5
2
Present at index 5
2 # of s27# of s28

Present at index 5
0def0
Present at index 5
2 def2

Present at index 5
0def4
Present at index 5
2 def6

Present at index 5
0def8
Present at index 5
2 isSubstring(s1, s2):0

Present at index 5
0# a string is substring of other.6 def8
Present at index 5
2
Present at index 5
2
18
9# Python3 program to check if 2 # of s28

# Python3 program to check if 5

Present at index 5
00
Present at index 5
01
Present at index 5
02
Present at index 5
03

Present at index 5
0
Present at index 5
05# of s28

# Python3 program to check if 5

Present at index 5
00
Present at index 5
01
Present at index 5
10 # Python3 program to check if 1
Present at index 5
12
Present at index 5
13

Present at index 5
14

đầu ra

Present at index 5

Phân tích độ phức tạp.  

  • Độ phức tạp về thời gian. O(m * n) trong đó m và n lần lượt là độ dài của s1 và s2.  
    Một vòng lặp lồng nhau được sử dụng, vòng lặp bên ngoài chạy từ 0 đến N-M và vòng lặp bên trong từ 0 đến M nên độ phức tạp là O(m*n)
  • Độ phức tạp không gian. Ô(1).  
    Vì không cần thêm dung lượng

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.
triển khai ngôn ngữ.  

  • Chuỗi con Java
  • chất nền trong C++
  • Python tìm

Một giải pháp hiệu quả khác.  

  • Một giải pháp hiệu quả sẽ chỉ cần một lần duyệt i. e. O(n) trên chuỗi dài hơn s1. Ở đây chúng ta sẽ bắt đầu duyệt chuỗi s1 và duy trì một con trỏ cho chuỗi s2 từ chỉ số thứ 0
  • Đối với mỗi lần lặp, chúng tôi so sánh ký tự hiện tại trong s1 và kiểm tra nó bằng con trỏ tại s2
  • Nếu chúng khớp nhau, chúng tôi tăng con trỏ trên s2 lên 1. Và đối với mỗi lần không khớp, chúng tôi đặt con trỏ về 0
  • Ngoài ra, hãy kiểm tra khi giá trị con trỏ s2 bằng độ dài của chuỗi s2, nếu đúng, chúng tôi ngắt và trả về giá trị (con trỏ của chuỗi s1 – con trỏ của chuỗi s2)
  • Hoạt động với các chuỗi chứa các ký tự trùng lặp

Python3




Present at index 5
15

def

Present at index 5
17
Present at index 5
18
Present at index 5
19

Present at index 5
20

Present at index 5
0
Present at index 5
22_______1_______2
Present at index 5
24

Present at index 5
0
Present at index 5
26
Present at index 5
2
Present at index 5
3
Present at index 5
01
Present at index 5
18
Present at index 5
03

Present at index 5
0
18
5
Present at index 5
2
Present at index 5
24

Present at index 5
20

Present at index 5
0
Present at index 5
38

Present at index 5
0
18
4
18
5
18
6
18
7
Present at index 5
01
Present at index 5
26# Python3 program to check if 3

# Python3 program to check if 5# a string is substring of other.6

Present at index 5
49
Present at index 5
2
Present at index 5
2
Present at index 5
3
Present at index 5
53

# a string is substring of other.5 3

# Python3 program to check if 5# a string is substring of other.6

Present at index 5
01
Present at index 5
18
Present at index 5
60
Present at index 5
2
Present at index 5
2
Present at index 5
63

# a string is substring of other.5

Present at index 5
22# Python3 program to check if 1
Present at index 5
2 # Python3 program to check if 2

# Python3 program to check if 5

Present at index 5
05# of s28

# a string is substring of other.5

Present at index 5
22
Present at index 5
2
Present at index 5
24

 4

Present at index 5
0# a string is substring of other.6
Present at index 5
79
Present at index 5
3_______1_______53

# Python3 program to check if 5# Returns true if s1 is substring 4

18
9# Python3 program to check if 2

Present at index 5
0
Present at index 5
05# of s28

# Python3 program to check if 5# Returns true if s1 is substring 4

Present at index 5
91
18
9
Present at index 5
93

 

Present at index 5
95

Present at index 5
00
Present at index 5
97
Present at index 5
98
Present at index 5
99
18
00
18
01

Present at index 5
00
Present at index 5
97
Present at index 5
98
Present at index 5
99
18
06
18
01

 

18
09

đầ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.