Answers for "longest consecutive subsequence"

C#
4

python longest consecutive sequence

def longest(seq = input("""Please enter a series of random letters,
i will guess the largest sequence of letters in order""").upper()):
    
    max_count = 0
    max_char = ""
    prev_char = ""
    
    for current in seq:
        if prev_char == current:
            count += 1
        else:
            count = 1
        if count > max_count:
            max_count = count
            max_char = current
        prev_char = current
    print(max_char,max_count)

longest()
Posted by: Guest on December-03-2020
1

longest consecutive subsequence

using System;
using System.Collections.Generic;
namespace Longest_Consecutive_Subsequence
{
    class Program
    {
        static void Main()
        {
            int[] arr = { 1,2,3,64,65,8 ,9};
            var maxSub = Subseq(arr, arr.Length);
            Console.WriteLine("Length   : "+ maxSub.Length);
            Console.Write("Elements : ");
            foreach (var item in maxSub)
            {
                Console.Write(item + " ");
            }
        }
        static int[] Subseq(int[] arr, int n)
        {
            Array.Sort(arr);
            
            // removing duplicate items from array

            var v = new List<int>
            {
                arr[0]
            };
            for (int i = 1; i < n; i++)
            {
                if (arr[i] != arr[i - 1])
                    v.Add(arr[i]);
            }

            // getting sub array
            var fin = new int[v.Count][];
            var tempList = new List<int>();
            var j = 0;
            for (int i = 0; i < v.Count; i++)
            {
                // adding first element if satisfy
                if (i == 0 && v[i] == v[i + 1] - 1)
                {
                    tempList.Add(v[i]);
                }
                else if (i > 0 && v[i] == v[i - 1] + 1)
                {
                    tempList.Add(v[i]);
                }
                // this will execute after continuous chain is breaked
                else if (i > 0 && v[i] != v[i - 1] + 1 && v[i] == v[i - 1] + 1 && i != v.Count - 1)
                {
                    tempList.Add(v[i]);
                }
                else
                {
                    fin[j] = new int[tempList.Count];
                    int m = 0;
                    foreach (var item in tempList)
                    {
                        fin[j][m++] = item;
                    }
                    tempList.Clear();
                    j++;
                }
            }

            // finding largest array in array
            var current = fin[0];
            for (var i = 0; i < j; i++)
            {
                if (fin[i].Length >= current.Length)
                {
                    current = fin[i];
                }
            }
            return current;
        }
    }
}
Posted by: Guest on September-07-2021
1

longest consecutive subsequence

static int[] Subseq(int[] arr, int n)
        {
            Array.Sort(arr);
            
            // removing duplicate items from array

            var v = new List<int>
            {
                arr[0]
            };
            for (int i = 1; i < n; i++)
            {
                if (arr[i] != arr[i - 1])
                    v.Add(arr[i]);
            }

            // getting sub array
            var fin = new int[v.Count][];
            var tempList = new List<int>();
            var j = 0;
            for (int i = 0; i < v.Count; i++)
            {
                // adding first element if satisfy
                if (i == 0 && v[i] == v[i + 1] - 1)
                {
                    tempList.Add(v[i]);
                }
                else if (i > 0 && v[i] == v[i - 1] + 1)
                {
                    tempList.Add(v[i]);
                }
                // this will execute after continuous chain is breaked
                else if (i > 0 && v[i] != v[i - 1] + 1 && v[i] == v[i - 1] + 1 && i != v.Count - 1)
                {
                    tempList.Add(v[i]);
                }
                else
                {
                    fin[j] = new int[tempList.Count];
                    int m = 0;
                    foreach (var item in tempList)
                    {
                        fin[j][m++] = item;
                    }
                    tempList.Clear();
                    j++;
                }
            }

            // finding largest array in array
            var current = fin[0];
            for (var i = 0; i < j; i++)
            {
                if (fin[i].Length > current.Length)
                {
                    current = fin[i];
                }
            }
            return current;
        }
Posted by: Guest on September-07-2021
3

longest common subsequence

class Solution:
    def longestCommonSubsequence(self, text1: str, text2: str) -> int:
        """
        text1: horizontally
        text2: vertically
        """
        dp = [[0 for _ in range(len(text1)+1)] for _ in range(len(text2)+1)]
        
        for row in range(1, len(text2)+1):
            for col in range(1, len(text1)+1):
                if text2[row-1]==text1[col-1]:
                    dp[row][col] = 1+ dp[row-1][col-1]
                else:
                    dp[row][col] = max(dp[row-1][col], dp[row][col-1])
        return dp[len(text2)][len(text1)]
Posted by: Guest on November-24-2020
2

longest common subsequence

int maxSubsequenceSubstring(char x[], char y[], 
                            int n, int m) 
{ 
    int dp[MAX][MAX]; 
  
    // Initialize the dp[][] to 0. 
    for (int i = 0; i <= m; i++) 
        for (int j = 0; j <= n; j++) 
            dp[i][j] = 0; 
  
    // Calculating value for each element. 
    for (int i = 1; i <= m; i++) { 
        for (int j = 1; j <= n; j++) { 
  
            // If alphabet of string X and Y are 
            // equal make dp[i][j] = 1 + dp[i-1][j-1] 
            if (x[j - 1] == y[i - 1]) 
                dp[i][j] = 1 + dp[i - 1][j - 1]; 
  
            // Else copy the previous value in the 
            // row i.e dp[i-1][j-1] 
            else
                dp[i][j] = dp[i][j - 1]; 
        } 
    } 
  
    // Finding the maximum length. 
    int ans = 0; 
    for (int i = 1; i <= m; i++) 
        ans = max(ans, dp[i][n]); 
  
    return ans; 
}
Posted by: Guest on May-31-2020

Code answers related to "longest consecutive subsequence"

C# Answers by Framework

Browse Popular Code Answers by Language