Cho chuỗi str và patt, tìm ký tự trong patt xuất hiện tại chỉ số nhỏ nhất trong str. Nếu không có ký tự nào chung, trả về -1.
Ví dụ: str="helloworld", patt="wor" → 'o' (chỉ số 4 trong str).
Giải thuật
- Duyệt qua từng ký tự trong patt.
- Với mỗi ký tự, tìm chỉ số đầu tiên xuất hiện trong str.
- Giữ ký tự có chỉ số nhỏ nhất.
Ví dụ
def get_min_index_char(Str, patt):
min_index = 10**9
for ch in patt:
idx = Str.find(ch)
if idx != -1 and idx < min_index:
min_index = idx
return Str[min_index] if min_index != 10**9 else -1
Str = "helloworld"
patt = "wor"
print(get_min_index_char(Str, patt))Đầu vào
"helloworld", "wor"
Đầu ra
o