title: Lcs Formed Consecutive Segments Least Length K
signature: class Solution {
  
      public Integer lcsFormedConsecutiveSegmentsLeastLengthK(Integer k, String s1, String s2) {
source_py: def lcs_formed_consecutive_segments_least_length_k(k, s1, s2):
      n = len(s1)
      m = len(s2)
      lcs = [[0 for x in range((m + 1))] for y in range((n + 1))]
      cnt = [[0 for x in range((m + 1))] for y in range((n + 1))]
      for i in range(1, (n + 1)):
          for j in range(1, (m + 1)):
              lcs[i][j] = max(lcs[(i - 1)][j], lcs[i][(j - 1)])
              if (s1[(i - 1)] == s2[(j - 1)]):
                  cnt[i][j] = (cnt[(i - 1)][(j - 1)] + 1)
              if (cnt[i][j] >= k):
                  for a in range(k, (cnt[i][j] + 1)):
                      lcs[i][j] = max(lcs[i][j], (lcs[(i - a)][(j - a)] + a))
      return lcs[n][m]
source_cpp: int lcs_formed_consecutive_segments_least_length_k(int k, string s1, string s2) {
    int n = s1 . length ( );
    int m = s2 . length ( );
    int lcs [ n + 1 ] [ m + 1 ];
    int cnt [ n + 1 ] [ m + 1 ];
    memset ( lcs, 0, sizeof ( lcs ) );
    memset ( cnt, 0, sizeof ( cnt ) );
    for ( int i = 1;
    i <= n;
    i ++ ) {
      for ( int j = 1;
      j <= m;
      j ++ ) {
        lcs [ i ] [ j ] = max ( lcs [ i - 1 ] [ j ], lcs [ i ] [ j - 1 ] );
        if ( s1 [ i - 1 ] == s2 [ j - 1 ] ) cnt [ i ] [ j ] = cnt [ i - 1 ] [ j - 1 ] + 1;
        if ( cnt [ i ] [ j ] >= k ) {
          for ( int a = k;
          a <= cnt [ i ] [ j ];
          a ++ ) lcs [ i ] [ j ] = max ( lcs [ i ] [ j ], lcs [ i - a ] [ j - a ] + a );
        }
      }
    }
    return lcs [ n ] [ m ];
  }
question_info.extension: java
question_info.entry_fn_name: lcsFormedConsecutiveSegmentsLeastLengthK
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 8, "inputs": {"k": 4, "s1": "aggayxysdfa", "s2": "aggajxaaasdfa"}}, {"idx": 1, "outputs": 2, "inputs": {"k": 2, "s1": "55571659965107", "s2": "390286654154"}}, {"idx": 2, "outputs": 7, "inputs": {"k": 3, "s1": "01011011100", "s2": "0000110001000"}}, {"idx": 3, "outputs": 5, "inputs": {"k": 5, "s1": "aggasdfa", "s2": "aggajasdfaxy"}}, {"idx": 4, "outputs": 0, "inputs": {"k": 2, "s1": "5710246551", "s2": "79032504084062"}}, {"idx": 5, "outputs": 5, "inputs": {"k": 3, "s1": "0100010", "s2": "10100000"}}, {"idx": 6, "outputs": 4, "inputs": {"k": 3, "s1": "aabcaaaa", "s2": "baaabcd"}}, {"idx": 7, "outputs": 3, "inputs": {"k": 1, "s1": "1219", "s2": "3337119582"}}, {"idx": 8, "outputs": 3, "inputs": {"k": 2, "s1": "111000011", "s2": "011"}}, {"idx": 9, "outputs": 0, "inputs": {"k": 2, "s1": "wiC oD", "s2": "csiuGOUwE"}}]
question_info.test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(Integer k, String s1, String s2, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(k, s1, s2,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution(4, "aggayxysdfa", "aggajxaaasdfa", 8);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution(2, "55571659965107", "390286654154", 2);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution(3, "01011011100", "0000110001000", 7);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution(5, "aggasdfa", "aggajasdfaxy", 5);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution(2, "5710246551", "79032504084062", 0);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution(3, "0100010", "10100000", 5);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution(3, "aabcaaaa", "baaabcd", 4);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution(1, "1219", "3337119582", 3);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution(2, "111000011", "011", 3);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution(2, "wiC oD", "csiuGOUwE", 0);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(Integer k, String s1, String s2, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(k, s1, s2),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: java __FILENAME__