title: Longest Prefix Also Suffix 1
signature: longest_prefix_also_suffix_1 <- function(s) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(s, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(s), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          "aabcdaabc", 
          4L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          "1372494598", 
          0L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          "110000100001", 
          1L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          "abcab", 
          2L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          "488938", 
          0L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          "011010101011", 
          3L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          "aaaa", 
          2L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          "3356203205", 
          0L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          "1010", 
          2L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          "kkXiiTZkGeh", 
          0L
      )))
  
  }
  
  main()
source_py: def longest_prefix_also_suffix_1(s):
      n = len(s)
      lps = ([0] * n)
      l = 0
      i = 1
      while (i < n):
          if (s[i] == s[l]):
              l = (l + 1)
              lps[i] = l
              i = (i + 1)
          elif (l != 0):
              l = lps[(l - 1)]
          else:
              lps[i] = 0
              i = (i + 1)
      res = lps[(n - 1)]
      if (res > (n / 2)):
          return (n // 2)
      else:
          return res
source_cpp: int longest_prefix_also_suffix_1(string s) {
    int n = s . length ( );
    int lps [ n ];
    lps [ 0 ] = 0;
    int len = 0;
    int i = 1;
    while ( i < n ) {
      if ( s [ i ] == s [ len ] ) {
        len ++;
        lps [ i ] = len;
        i ++;
      }
      else {
        if ( len != 0 ) {
          len = lps [ len - 1 ];
        }
        else {
          lps [ i ] = 0;
          i ++;
        }
      }
    }
    int res = lps [ n - 1 ];
    return ( res > n / 2 ) ? n / 2 : res;
  }