দুটি স্ট্রিং source এবং target দেওয়া আছে। source-এর সাবসিকোয়েন্সগুলো কনক্যাটেনেট করে target বানাতে হবে। ন্যূনতম কতটি সাবসিকোয়েন্স লাগবে তা বের করুন। না পারলে -1 রিটার্ন করুন।
উদাহরণ: source = "xyz", target = "xyzyzz" → আউটপুট ৩ ("xyz" + "yz" + "z")।
সমাধানের ধাপগুলো:
s_size, t_sizeলেনথ।concat_count = 0, target_idx = 0।while target_idx < t_size:source_idx = 0, temp_index = target_idx।- সোর্স ও টার্গেট স্ক্যান করে ম্যাচ করান।
- যদি
temp_index == target_idxহয় তবে -1 রিটার্ন করুন। concat_count++।- শেষে
concat_countরিটার্ন করুন।
উদাহরণ
class Solution:
def solve(self, s, t):
s_size, t_size = len(s), len(t)
concat_count = 0
target_idx = 0
while target_idx < t_size:
source_idx = 0
temp_index = target_idx
while source_idx < s_size and target_idx < t_size:
if s[source_idx] == t[target_idx]:
target_idx += 1
source_idx += 1
if temp_index == target_idx:
return -1
concat_count += 1
return concat_count
ob = Solution()
source = "xyz"
target = "xyzyzz"
print(ob.solve(source, target))
ইনপুট
"xyz", "xyzyzz"
আউটপুট
3