Length of longest increasing subsequence in a string
Last Updated :
18 Jan, 2023
Given a string S, the task is to find the length of the longest increasing subsequence present in the given string.
A sequence of characters placed in increasing order of their ASCII values is called an increasing sequence.
Examples:
Input: S = “abcfgffs”
Output: 6
Explanation: Subsequence “abcfgs” is the longest increasing subsequence present in the string. Therefore, the length of the longest increasing subsequence is 6.
Input: S = “aaabac”
Output: 3
Approach: The idea is to use Dynamic Programming. Follow the steps given below to solve the problem:
- Initialize an array, say dp[] of size 26, to store at every ith index, the length of the longest increasing subsequence having (‘a’ + i)th character as the last character in the subsequence.
- Initialize variable, say lis, to store the length of the required subsequence.
- Iterate over each character of the string S.
- For every character encountered, i.e. S[i] – ‘a’, check for all characters, say j, with ASCII values smaller than that of the current character.
- Initialize a variable, say curr, to store the length of LIS ending with current character.
- Update curr with max(curr, dp[j]).
- Update length of the LIS, say lis, with max(lis, curr + 1).
- Update dp[S[i] – ‘a’] with max of d[S[i] – ‘a’] and curr.
- Finally, print the value of lis as the required length of LIS.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int lisOtimised(string s)
{
int dp[30] = { 0 };
int N = s.size();
int lis = INT_MIN;
for (int i = 0; i < N; i++) {
int val = s[i] - 'a';
int curr = 0;
for (int j = 0; j < val; j++) {
curr = max(curr, dp[j]);
}
curr++;
lis = max(lis, curr);
dp[val] = max(dp[val], curr);
}
return lis;
}
int main()
{
string s = "fdryutiaghfse";
cout << lisOtimised(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mn = -2147483648;
static int lisOtimised(String s)
{
int []dp = new int[30];
Arrays.fill(dp, 0);
int N = s.length();
int lis = mn;
for(int i = 0; i < N; i++)
{
int val = (int)s.charAt(i) - 97;
int curr = 0;
for(int j = 0; j < val; j++)
{
curr = Math.max(curr, dp[j]);
}
curr++;
lis = Math.max(lis, curr);
dp[val] = Math.max(dp[val], curr);
}
return lis;
}
public static void main(String[] args)
{
String s = "fdryutiaghfse";
System.out.print(lisOtimised(s));
}
}
|
Python3
def lisOtimised(s):
dp = [0]*30
N = len(s)
lis = -10**9
for i in range(N):
val = ord(s[i]) - ord('a')
curr = 0
for j in range(val):
curr = max(curr, dp[j])
curr += 1
lis = max(lis, curr)
dp[val] = max(dp[val], curr)
return lis
if __name__ == '__main__':
s = "fdryutiaghfse"
print (lisOtimised(s))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int mn = -2147483648;
static int lisOtimised(string s)
{
int []dp = new int[30];
Array.Clear(dp, 0, 30);
int N = s.Length;
int lis = mn;
for (int i = 0; i < N; i++) {
int val = (int)s[i] - 97;
int curr = 0;
for (int j = 0; j < val; j++) {
curr = Math.Max(curr, dp[j]);
}
curr++;
lis = Math.Max(lis, curr);
dp[val] = Math.Max(dp[val], curr);
}
return lis;
}
public static void Main()
{
string s = "fdryutiaghfse";
Console.Write(lisOtimised(s));
}
}
|
Javascript
<script>
function lisOtimised( s)
{
let dp = new Array(30).fill(0);
let N = s.length;
let lis = Number.MIN_VALUE;
for (let i = 0; i < N; i++) {
let val = s.charCodeAt(i) - 'a'.charCodeAt(0);
let curr = 0;
for (let j = 0; j < val; j++) {
curr = Math.max(curr, dp[j]);
}
curr++;
lis = Math.max(lis, curr);
dp[val] = Math.max(dp[val], curr);
}
return lis;
}
let s = "fdryutiaghfse";
document.write(lisOtimised(s));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...