title: Longest Repeated Subsequence 1
signature: function longestRepeatedSubsequence1(str: string): string {
test_code: 
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  function convertToString(value: any): string{
      return JSON.stringify(value, function (key, val) {
          if (val instanceof Set) {
              return Array.from(val);
          } return val;});
  }
  
  function validateSolution(actual: string, expected: string): boolean {
      return convertToString(actual) === convertToString(expected);
  }
  
  function driver(str: string, expected:string): string {
      try{
          if (validateSolution(PLACEHOLDER_FN_NAME(str),expected)){
              return "PASSED";
          }
          return "FAILED";
      } catch (error) {
        return error.name;
      }
      
  }
      
  function main(){
    let result = '';
    result = driver("qnQxjoRx", "x");
    console.log(`TEST-0...${result}`);
  
    result = driver("27473733400077", "7733007");
    console.log(`TEST-1...${result}`);
  
    result = driver("000010111111", "0000111111");
    console.log(`TEST-2...${result}`);
  
    result = driver("TNVwgrWSLu", "");
    console.log(`TEST-3...${result}`);
  
    result = driver("9537", "");
    console.log(`TEST-4...${result}`);
  
    result = driver("1100", "10");
    console.log(`TEST-5...${result}`);
  
    result = driver("lYcoiQfzN", "");
    console.log(`TEST-6...${result}`);
  
    result = driver("52", "");
    console.log(`TEST-7...${result}`);
  
    result = driver("00100001100", "0000010");
    console.log(`TEST-8...${result}`);
  
    result = driver("Rkxe", "");
    console.log(`TEST-9...${result}`);
  
  };
  
  main();
source_py: def longest_repeated_subsequence_1(str):
      n = len(str)
      dp = [[0 for i in range((n + 1))] for j in range((n + 1))]
      for i in range(1, (n + 1)):
          for j in range(1, (n + 1)):
              if ((str[(i - 1)] == str[(j - 1)]) and (i != j)):
                  dp[i][j] = (1 + dp[(i - 1)][(j - 1)])
              else:
                  dp[i][j] = max(dp[i][(j - 1)], dp[(i - 1)][j])
      res = ''
      i = n
      j = n
      while ((i > 0) and (j > 0)):
          if (dp[i][j] == (dp[(i - 1)][(j - 1)] + 1)):
              res += str[(i - 1)]
              i -= 1
              j -= 1
          elif (dp[i][j] == dp[(i - 1)][j]):
              i -= 1
          else:
              j -= 1
      res = ''.join(reversed(res))
      return res
source_cpp: string longest_repeated_subsequence_1(string str) {
    int n = str . length ( );
    int dp [ n + 1 ] [ n + 1 ];
    for ( int i = 0;
    i <= n;
    i ++ ) for ( int j = 0;
    j <= n;
    j ++ ) dp [ i ] [ j ] = 0;
    for ( int i = 1;
    i <= n;
    i ++ ) for ( int j = 1;
    j <= n;
    j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ];
    else dp [ i ] [ j ] = max ( dp [ i ] [ j - 1 ], dp [ i - 1 ] [ j ] );
    string res = "";
    int i = n, j = n;
    while ( i > 0 && j > 0 ) {
      if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j - 1 ] + 1 ) {
        res = res + str [ i - 1 ];
        i --;
        j --;
      }
      else if ( dp [ i ] [ j ] == dp [ i - 1 ] [ j ] ) i --;
      else j --;
    }
    reverse ( res . begin ( ), res . end ( ) );
    return res;
  }