দুটি স্ট্রিং দেওয়া আছে। লেক্সিকোগ্রাফিক্যালি মিনিমাল স্ট্রিং বানাতে হবে: প্রতিবার দুটি স্ট্রিং-এর প্রথম ক্যারেক্টার কম্পেয়ার করে ছোটটি নিন। টাই হলে প্রথম স্ট্রিং-এর ক্যারেক্টার নিন। দুটি স্ট্রিং খালি না হওয়া পর্যন্ত চালিয়ে যান।
উদাহরণ
input_1 = 'TUTORIALS', input_2 = 'POINT' → আউটপুট: POINTTUTORIALS
স্টেপ-বাই-স্টেপ:
TUTORIALS POINT → P
TUTORIALS OINT → PO
TUTORIALS INT → POI
TUTORIALS NT → POIN
TUTORIALS T → POINT
TUTORIALS (খালি) → POINTTUTORIALS
এলগরিদম
- দুটি স্ট্রিংয়ের শেষে
'z'অ্যাপেন্ড করুন (সেন্টিনেল)। temp_1 := 0, temp_2 := 0, res_str := ""while temp_1 < len(input_1) and temp_2 < len(input_2):- যদি
input_1[temp_1:] < input_2[temp_2:]:res_str += input_1[temp_1], temp_1++ - অন্যথায়:
res_str += input_2[temp_2], temp_2++
- যদি
- সেন্টিনেল
'z'রিমুভ করুন। - বাকি স্ট্রিং অপেন্ড করুন।
পাইথন কোড
def solve(input_1, input_2):
input_1 += "z"
input_2 += "z"
temp_1 = 0
temp_2 = 0
res_str = ""
while temp_1 < len(input_1) and temp_2 < len(input_2):
if input_1[temp_1:] < input_2[temp_2:]:
res_str += input_1[temp_1]
temp_1 += 1
else:
res_str += input_2[temp_2]
temp_2 += 1
res_str = res_str[:-1]
if temp_1 < len(input_1):
res_str += input_1[temp_1:-1]
if temp_2 < len(input_2):
res_str += input_2[temp_2:-1]
return res_str
print(solve('TUTORIALS', 'POINT')) # আউটপুট: POINTTUTORIALS
ইনপুট / আউটপুট
'TUTORIALS', 'POINT' POINTTUTORIALS