title: Longest Repeated Subsequence 1
signature: class Solution {
  
      public String longestRepeatedSubsequence1(String str) {
test_code: import static java.util.Map.entry;  
  import java.util.*;
  import java.lang.*;
  // TESTING CODE 
  // ============================================
  class QuestionEvaluator {
      public static String validateSolution(String str, String expected){
          try{
              Driver driver = new Driver();
              if (driver.run(str,expected)){
                  return "PASSED";
              }
              return "FAILED";
              
          } catch (Exception exception_obj){
              return exception_obj.getClass().getSimpleName();
          }
  
      }
      public static void main(String[] args) {
          String result;
          result = validateSolution("qnQxjoRx", "x");
          System.out.println(String.format("%s%s","TEST-0...",result));    
      
          result = validateSolution("27473733400077", "7733007");
          System.out.println(String.format("%s%s","TEST-1...",result));    
      
          result = validateSolution("000010111111", "0000111111");
          System.out.println(String.format("%s%s","TEST-2...",result));    
      
          result = validateSolution("TNVwgrWSLu", "");
          System.out.println(String.format("%s%s","TEST-3...",result));    
      
          result = validateSolution("9537", "");
          System.out.println(String.format("%s%s","TEST-4...",result));    
      
          result = validateSolution("1100", "10");
          System.out.println(String.format("%s%s","TEST-5...",result));    
      
          result = validateSolution("lYcoiQfzN", "");
          System.out.println(String.format("%s%s","TEST-6...",result));    
      
          result = validateSolution("52", "");
          System.out.println(String.format("%s%s","TEST-7...",result));    
      
          result = validateSolution("00100001100", "0000010");
          System.out.println(String.format("%s%s","TEST-8...",result));    
      
          result = validateSolution("Rkxe", "");
          System.out.println(String.format("%s%s","TEST-9...",result));    
      
      }
  }
  
  class Driver{
      public boolean run(String str, String expected){
  
          PLACEHOLDER_CLS_NAME sol = new PLACEHOLDER_CLS_NAME();
          return validateSolution(sol.PLACEHOLDER_FN_NAME(str),expected);
      }
      
      public boolean validateSolution(String actual, String expected){
          return actual.equals(expected);
      }
  }
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
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;
  }