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 527
Present at index 528
Present at index 529
Present at index 530
Present at index 531
Present at index 532
Present at index 533
Present at index 50
Present at index 51
Present at index 52
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 529
Present at index 50
Present at index 522
Present at index 50
Present at index 524
Present at index 525
Present at index 526
Present at index 527
Present at index 528
Present at index 529
Present at index 51
Present at index 5271
Present at index 5272
Present at index 5273
Present at index 529
Present at index 5275
Present at index 5276
Present at index 5275
Present at index 5278
Present at index 5275
Present at index 524
Present at index 5281
Present at index 526
Present at index 527
Present at index 5284
Present at index 5285
Present at index 5286
Present at index 5287
Present at index 5271
Present at index 5289
Present at index 52
Present at index 5291
Present at index 5292
Present at index 5293
Present at index 5294
Present at index 5275
Present at index 5286
Present at index 5281
Present at index 5271
Present at index 5272
Present at index 52
Present at index 52
Present at index 5302
Present at index 5285
Present at index 5304
Present at index 525
Present at index 529
Present at index 50____1304
Present at index 529____1272
Present at index 529
Present at index 5312
Present at index 5286
Present at index 5314
Present at index 52
Present at index 52
Present at index 5317
Present at index 5318
Present at index 50
Present at index 5320
Present at index 52
Present at index 5322
Present at index 50
Present at index 5324
Present at index 52
Present at index 5326
Present at index 50
Present at index 5328
Present at index 52
Present at index 5330
Present at index 50_______1286
Present at index 5328
Present at index 52
Present at index 52
Present at index 529
Present at index 5272
Present at index 5318
Present at index 5275
Present at index 500
Present at index 501
Present at index 502
Present at index 503
Present at index 50____105____1318
Present at index 5275
Present at index 500
Present at index 501
Present at index 510
Present at index 5271
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.
- Độ 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 515
Present at index 532
Present at index 517
Present at index 518
Present at index 519
Present at index 520
Present at index 50
Present at index 522
Present at index 52
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
Present at index 525
Present at index 52
Present at index 524
Present at index 520
Present at index 50
Present at index 538
Present at index 50
Present at index 524
Present at index 525
Present at index 526
Present at index 527
Present at index 501
Present at index 526
Present at index 5273
Present at index 5275
Present at index 5286
Present at index 549
Present at index 52
Present at index 52
Present at index 53
Present at index 553
Present at index 5285
Present at index 5293
Present at index 5275
Present at index 5286
Present at index 501____118
Present at index 560
Present at index 52
Present at index 52
Present at index 563
Present at index 5285
Present at index 522
Present at index 5271
Present at index 52
Present at index 5272
Present at index 5275
Present at index 505
Present at index 5318
Present at index 5285
Present at index 522
Present at index 52
Present at index 524
Present at index 5294
Present at index 50
Present at index 5286
Present at index 579____13
Present at index 553
Present at index 5275
Present at index 5304
Present at index 529____1272
Present at index 50____105____1318
Present at index 5275
Present at index 5304
Present at index 591____129
Present at index 593
Present at index 529
Present at index 595
Present at index 500
Present at index 597____198
Present at index 599
Present at index 5200
Present at index 5201
Present at index 500
Present at index 597____198
Present at index 599
Present at index 5206
Present at index 5201
Present at index 529
Present at index 5209
đầu ra
Present at index 52
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]