Software Development Exam  >  Software Development Questions  >  Given two strings s1 and s2, find the length ... Start Learning for Free
Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:
def longest_common_substring(s1, s2):
    # Your code here
string1 = "ABCD"
string2 = "ACDF"
print(longest_common_substring(string1, string2))
  • a)
    1
  • b)
    2
  • c)
    3
  • d)
    4
Correct answer is option 'B'. Can you explain this answer?
Verified Answer
Given two strings s1 and s2, find the length of their longest common s...
The function longest_common_substring should return the length of the longest common substring between the strings "ABCD" and "ACDF". The longest common substring is "CD", which has a length of 2.
View all questions of this test
Most Upvoted Answer
Given two strings s1 and s2, find the length of their longest common s...
Understanding the Problem
To find the longest common substring between two strings, we need to identify the longest sequence of characters that appears in both strings consecutively.
Example Strings
- string1: "ABCD"
- string2: "ACDF"
Identifying Common Substrings
We can analyze the characters in both strings to find matches:
- "A" is common in both strings.
- "C" is also common in both strings.
- However, "B" and "D" do not have consecutive matches.
Finding the Longest Common Substring
The common substrings between "ABCD" and "ACDF" are:
- "A" (length 1)
- "C" (length 1)
The longest common substring (consecutively appearing in both) is "AC", which has a length of 2.
Implementing the Function
Here is a simple implementation of the function to find the longest common substring:
python
def longest_common_substring(s1, s2):
m, n = len(s1), len(s2)
max_length = 0
# Create a 2D array to store lengths of longest common suffixes
dp = [[0] * (n + 1) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
max_length = max(max_length, dp[i][j])
else:
dp[i][j] = 0
return max_length
# Example usage
string1 = "ABCD"
string2 = "ACDF"
print(longest_common_substring(string1, string2))
Conclusion
The length of the longest common substring between "ABCD" and "ACDF" is 2, corresponding to the substring "AC". Therefore, the correct answer is option 'B'.
Explore Courses for Software Development exam

Top Courses for Software Development

Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer?
Question Description
Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? for Software Development 2025 is part of Software Development preparation. The Question and answers have been prepared according to the Software Development exam syllabus. Information about Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? covers all topics & solutions for Software Development 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer?.
Solutions for Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? in English & in Hindi are available as part of our courses for Software Development. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.
Here you can find the meaning of Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer?, a detailed solution for Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? has been provided alongside types of Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Given two strings s1 and s2, find the length of their longest common substring. Implement the following function in Python:def longest_common_substring(s1, s2): # Your code herestring1 = "ABCD"string2 = "ACDF"print(longest_common_substring(string1, string2))a)1b)2c)3d)4Correct answer is option 'B'. Can you explain this answer? tests, examples and also practice Software Development tests.
Explore Courses for Software Development exam

Top Courses for Software Development

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev