title: Space Optimized Solution Lcs
signature: func spaceOptimizedSolutionLcs(x string, y string) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(x string, y string, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(x, y),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver("YNpjSv", 
  	"qtUkJn", 0)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver("736519", 
  	"07592", 2)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver("11010000100010", 
  	"0", 1)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver("v ", 
  	"qGBQT", 0)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver("8311172", 
  	"157219329531", 3)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver("100011101011", 
  	"1000001111", 7)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver("u", 
  	"YzkubTqLhP", 0)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver("3943042", 
  	"3859", 1)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver("101", 
  	"00010000101010", 2)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver("MpbfF OsizevaM", 
  	"WgsFGaQwtp", 2)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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 ];
  }