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 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
# 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
184
# a string is substring of other.
1186
187
# 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 52
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 52
Present at index 52
# Returns true if s1 is substring
2# a string is substring of other.
5# Returns true if s1 is substring
4
185
Present at index 50
# Returns true if s1 is substring
4 189
# Python3 program to check if
2
# of s2
2
# a string is substring of other.
6 # of s2
4
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# Python3 program to check if
5
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# Python3 program to check if
5
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.
- Độ 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 515
def
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# Python3 program to check if
5# a string is substring of other.
6
Present at index 549
Present at index 52
Present at index 52
Present at index 53
Present at index 553
# a string is substring of other.
5
3
# Python3 program to check if
5# a string is substring of other.
6
Present at index 501
Present at index 518
Present at index 560
Present at index 52
Present at index 52
Present at index 563
# a string is substring of other.
5
Present at index 522
# Python3 program to check if
1Present at index 52
# Python3 program to check if
2# Python3 program to check if
5
Present at index 505
# of s2
8# a string is substring of other.
5
Present at index 522
Present at index 52
Present at index 524
4
Present at index 50
# a string is substring of other.
6 Present at index 579
Present at index 53_______1_______53
# Python3 program to check if
5# Returns true if s1 is substring
4
189
# Python3 program to check if
2Present at index 50
Present at index 505
# of s2
8# Python3 program to check if
5# Returns true if s1 is substring
4
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]