Tap here to switch tabs
Problem
Submissions
Solution

Solution: Longest Common Subsequence

Statement

Naive approach

A naive approach would be to compare the characters of both strings based on the following rules:

  • If the current characters of both strings match, we move one position ahead in both strings.

  • If the current characters of both strings do not match, we recursively calculate the maximum length of moving one character forward in any one of the two strings i.e., we check if moving a character forward in either the first string or the second will give us a longer subsequence.

  • If we reach the end of either of the two strings, we return 00.

The time complexity of the naive approach is O(2m+n)O(2^{m+n}) ...

Tap here to switch tabs
Problem
Submissions
Solution

Solution: Longest Common Subsequence

Statement

Naive approach

A naive approach would be to compare the characters of both strings based on the following rules:

  • If the current characters of both strings match, we move one position ahead in both strings.

  • If the current characters of both strings do not match, we recursively calculate the maximum length of moving one character forward in any one of the two strings i.e., we check if moving a character forward in either the first string or the second will give us a longer subsequence.

  • If we reach the end of either of the two strings, we return 00.

The time complexity of the naive approach is O(2m+n)O(2^{m+n}) ...