title: Space Optimized Solution Lcs
signature: function space_optimized_solution_lcs(x, y)
test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(x, y, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(x, y) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver("YNpjSv", "qtUkJn", 0)
      print(string.format("TEST-0...%s",result))
  
      result = driver("736519", "07592", 2)
      print(string.format("TEST-1...%s",result))
  
      result = driver("11010000100010", "0", 1)
      print(string.format("TEST-2...%s",result))
  
      result = driver("v ", "qGBQT", 0)
      print(string.format("TEST-3...%s",result))
  
      result = driver("8311172", "157219329531", 3)
      print(string.format("TEST-4...%s",result))
  
      result = driver("100011101011", "1000001111", 7)
      print(string.format("TEST-5...%s",result))
  
      result = driver("u", "YzkubTqLhP", 0)
      print(string.format("TEST-6...%s",result))
  
      result = driver("3943042", "3859", 1)
      print(string.format("TEST-7...%s",result))
  
      result = driver("101", "00010000101010", 2)
      print(string.format("TEST-8...%s",result))
  
      result = driver("MpbfF OsizevaM", "WgsFGaQwtp", 2)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
source_py: def space_optimized_solution_lcs(X, Y):
      m = len(X)
      n = len(Y)
      L = [[0 for i in range((n + 1))] for j in range(2)]
      bi = bool
      for i in range(m):
          bi = (i & 1)
          for j in range((n + 1)):
              if ((i == 0) or (j == 0)):
                  L[bi][j] = 0
              elif (X[i] == Y[(j - 1)]):
                  L[bi][j] = (L[(1 - bi)][(j - 1)] + 1)
              else:
                  L[bi][j] = max(L[(1 - bi)][j], L[bi][(j - 1)])
      return L[bi][n]
source_cpp: int space_optimized_solution_lcs(string & X, string & Y) {
    int m = X . length ( ), n = Y . length ( );
    int L [ 2 ] [ n + 1 ];
    bool bi;
    for ( int i = 0;
    i <= m;
    i ++ ) {
      bi = i & 1;
      for ( int j = 0;
      j <= n;
      j ++ ) {
        if ( i == 0 || j == 0 ) L [ bi ] [ j ] = 0;
        else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ bi ] [ j ] = L [ 1 - bi ] [ j - 1 ] + 1;
        else L [ bi ] [ j ] = max ( L [ 1 - bi ] [ j ], L [ bi ] [ j - 1 ] );
      }
    }
    return L [ bi ] [ n ];
  }