title: Space Optimized Solution Lcs
signature: class Solution {
  
      public Integer spaceOptimizedSolutionLcs(String x, String y) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(String x, String y, Integer expected){
          try{
              Driver driver = new Driver();
              if (driver.run(x, y,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution("YNpjSv", "qtUkJn", 0);
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution("736519", "07592", 2);
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution("11010000100010", "0", 1);
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution("v ", "qGBQT", 0);
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution("8311172", "157219329531", 3);
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution("100011101011", "1000001111", 7);
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution("u", "YzkubTqLhP", 0);
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution("3943042", "3859", 1);
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution("101", "00010000101010", 2);
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution("MpbfF OsizevaM", "WgsFGaQwtp", 2);
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(String x, String y, Integer expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(x, y),expected);
      }
      
      public boolean validateSolution(Integer actual, Integer expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
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 ];
  }