Độ phức tạp thời gian của chuỗi Python

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 độ dài N và một xâu con độ 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




Present at index 5
27

Present at index 5
28

Present at index 5
29

Present at index 5
30

Present at index 5
31

Present at index 5
32
Present at index 5
33

Present at index 5
0
Present at index 5
1
Present at index 5
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
29

Present at index 5
0
Present at index 5
22

Present at index 5
0
Present at index 5
24
Present at index 5
25
Present at index 5
26
Present at index 5
27
Present at index 5
28
Present at index 5
29
Present at index 5
1
Present at index 5
271
Present at index 5
272
Present at index 5
273

Present at index 5
29

Present at index 5
275
Present at index 5
276

Present at index 5
275
Present at index 5
278

Present at index 5
275
Present at index 5
24
Present at index 5
281
Present at index 5
26
Present at index 5
27
Present at index 5
284

Present at index 5
285
Present at index 5
286
Present at index 5
287
Present at index 5
271
Present at index 5
289
Present at index 5
2
Present at index 5
291

Present at index 5
292
Present at index 5
293

Present at index 5
294

Present at index 5
275
Present at index 5
286
Present at index 5
281
Present at index 5
271
Present at index 5
272
Present at index 5
2
Present at index 5
2
Present at index 5
302

Present at index 5
285
Present at index 5
304
Present at index 5
25

Present at index 5
29

Present at index 5
0____1304
Present at index 5
29____1272

Present at index 5
29

Present at index 5
312

Present at index 5
286
Present at index 5
314
Present at index 5
2
Present at index 5
2
Present at index 5
317
Present at index 5
318

Present at index 5
0
Present at index 5
320
Present at index 5
2
Present at index 5
322

Present at index 5
0
Present at index 5
324
Present at index 5
2
Present at index 5
326

Present at index 5
0
Present at index 5
328
Present at index 5
2
Present at index 5
330

Present at index 5
0_______1286
Present at index 5
328
Present at index 5
2
Present at index 5
2
Present at index 5
29
Present at index 5
272
Present at index 5
318

Present at index 5
275
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____105____1318

Present at index 5
275
Present at index 5
00
Present at index 5
01
Present at index 5
10
Present at index 5
271
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 chạy từ 0 đến M nên độ phức tạp là O[m*n].
  • Độ phức tạp của 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

Present at index 5
32
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
Present at index 5
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
Present at index 5
25
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
Present at index 5
24
Present at index 5
25
Present at index 5
26
Present at index 5
27
Present at index 5
01
Present at index 5
26
Present at index 5
273

Present at index 5
275
Present at index 5
286
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

Present at index 5
285
Present at index 5
293

Present at index 5
275
Present at index 5
286
Present at index 5
01____118
Present at index 5
60
Present at index 5
2
Present at index 5
2
Present at index 5
63

Present at index 5
285
Present at index 5
22
Present at index 5
271
Present at index 5
2
Present at index 5
272

Present at index 5
275
Present at index 5
05
Present at index 5
318

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

Present at index 5
294

Present at index 5
0
Present at index 5
286
Present at index 5
79____13
Present at index 5
53

Present at index 5
275
Present at index 5
304
Present at index 5
29____1272

Present at index 5
0____105____1318

Present at index 5
275
Present at index 5
304
Present at index 5
91____129
Present at index 5
93

Present at index 5
29

Present at index 5
95

Present at index 5
00
Present at index 5
97____198
Present at index 5
99
Present at index 5
200
Present at index 5
201

Present at index 5
00
Present at index 5
97____198
Present at index 5
99
Present at index 5
206
Present at index 5
201

Present at index 5
29

Present at index 5
209

đầu ra

Present at index 5
2

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 về thời gian của * Một chuỗi trong Python là gì?

__str__ có độ phức tạp thời gian chạy là O[m*n] trong đó m là số chữ số nhị phân và n là số chữ số thập phân.

Độ phức tạp thời gian của chuỗi là gì?

Khi xem xét trường hợp chuỗi, nếu chuỗi khá lớn thì Độ phức tạp về thời gian sẽ là O[kích thước của chuỗi * Log[L]] mỗi lần chèn .

Độ phức tạp thời gian của nối chuỗi là gì?

Nối chuỗi . Lý do là trong Java, các chuỗi là bất biến và kết quả là mỗi khi bạn nối thêm vào chuỗi, đối tượng chuỗi mới được tạo. O[N2] time. The reason is that in Java strings are immutable, and as a result, each time you append to the string new string object is created.

Độ phức tạp của việc cắt chuỗi là gì?

Cắt lát chỉ là "sao chép một phần của danh sách" nên độ phức tạp về thời gian cũng giống như sao chép. O[n+k] là trường hợp trung bình, bao gồm việc phải tăng hoặc thu nhỏ danh sách để điều chỉnh số lượng phần tử được chèn vào để thay thế phần tử ban đầu .

Chủ Đề