title: Lcs Formed Consecutive Segments Least Length K
signature: pub fn lcs_formed_consecutive_segments_least_length_k(k: i32, s1: String, s2: String) -> i32 {
test_code: use std::collections::HashMap;
  use std::collections::HashSet;
  use std::cmp::*;
  use std::collections::*;
  use std::fmt::*; 
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  pub fn validate_solution(actual: i32, expected: i32)->bool{
  return actual==expected
  }
  pub fn driver(k: i32, s1: String, s2: String, expected: i32)->String{
      if validate_solution(PLACEHOLDER_FN_NAME(k, s1, s2), expected) {
        return "PASSED".to_string();
      };
      return "FAILED".to_string();
  
  }
  
  fn main() {
      println!("TEST-0...{}", driver(
        4, "aggayxysdfa".to_string(), "aggajxaaasdfa".to_string(), 
        8
      ));
      
      println!("TEST-1...{}", driver(
        2, "55571659965107".to_string(), "390286654154".to_string(), 
        2
      ));
      
      println!("TEST-2...{}", driver(
        3, "01011011100".to_string(), "0000110001000".to_string(), 
        7
      ));
      
      println!("TEST-3...{}", driver(
        5, "aggasdfa".to_string(), "aggajasdfaxy".to_string(), 
        5
      ));
      
      println!("TEST-4...{}", driver(
        2, "5710246551".to_string(), "79032504084062".to_string(), 
        0
      ));
      
      println!("TEST-5...{}", driver(
        3, "0100010".to_string(), "10100000".to_string(), 
        5
      ));
      
      println!("TEST-6...{}", driver(
        3, "aabcaaaa".to_string(), "baaabcd".to_string(), 
        4
      ));
      
      println!("TEST-7...{}", driver(
        1, "1219".to_string(), "3337119582".to_string(), 
        3
      ));
      
      println!("TEST-8...{}", driver(
        2, "111000011".to_string(), "011".to_string(), 
        3
      ));
      
      println!("TEST-9...{}", driver(
        2, "wiC oD".to_string(), "csiuGOUwE".to_string(), 
        0
      ));
      
  }
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 ];
  }