title: Longest Prefix Also Suffix 1
signature: int longestPrefixAlsoSuffix1(string s) {
test_code: #include <algorithm>
  #include <assert.h>
  #include <cstdlib>
  #include <iostream>
  #include <map>
  #include <set>
  #include <string>
  #include <tuple> 
  #include <unordered_map>
  #include <vector>
  #include <math.h>
  #include <cmath>
  #include <bits/stdc++.h>
  #include <numeric>
  #include <functional>
  
  using namespace std;
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  bool validateSolution(int actual, int expected){
      return actual == expected;
  }
  
  string driver(string s, int expected){
      try
      {
          if (validateSolution(PLACEHOLDER_FN_NAME(s), expected)){
              return "PASSED";
          }
          return "FAILED";
      }
      catch (const std::overflow_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::runtime_error& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (const std::exception& exception_obj)
      {
          return typeid(exception_obj).name();
      }
      catch (...)
      {
          return "UNK_ERROR";
      }
  }
  
  int main() {
      string result = "";
  
      result = driver("aabcdaabc", 4); 
      cout << "TEST-" << 0 << "..."
           <<  result
           << "\n";
  
      result = driver("1372494598", 0); 
      cout << "TEST-" << 1 << "..."
           <<  result
           << "\n";
  
      result = driver("110000100001", 1); 
      cout << "TEST-" << 2 << "..."
           <<  result
           << "\n";
  
      result = driver("abcab", 2); 
      cout << "TEST-" << 3 << "..."
           <<  result
           << "\n";
  
      result = driver("488938", 0); 
      cout << "TEST-" << 4 << "..."
           <<  result
           << "\n";
  
      result = driver("011010101011", 3); 
      cout << "TEST-" << 5 << "..."
           <<  result
           << "\n";
  
      result = driver("aaaa", 2); 
      cout << "TEST-" << 6 << "..."
           <<  result
           << "\n";
  
      result = driver("3356203205", 0); 
      cout << "TEST-" << 7 << "..."
           <<  result
           << "\n";
  
      result = driver("1010", 2); 
      cout << "TEST-" << 8 << "..."
           <<  result
           << "\n";
  
      result = driver("kkXiiTZkGeh", 0); 
      cout << "TEST-" << 9 << "..."
           <<  result
           << "\n";
  
      return 0;
  }
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;
  }