title: Longest Prefix Also Suffix 1
signature: fun longestPrefixAlsoSuffix1(s: String): Int {
test_code: package main
  
  import kotlin.text.*
  import kotlin.math.*
  
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  fun validateSolution(actual:Int, expected:Int):Boolean{
    return actual.equals(expected)
  }
  fun driver(s: String, expected:Int):String {
      try{
        if (validateSolution(PLACEHOLDER_FN_NAME(s), expected)) {
                return "PASSED"
        }
        return "FAILED"
      } catch (e: Exception){
        return e::class.simpleName as String
      }
  
  }
  
  fun main() {
      var result: String = ""
      result = driver("aabcdaabc", 4)
      println("TEST-0...${result}")
      
      result = driver("1372494598", 0)
      println("TEST-1...${result}")
      
      result = driver("110000100001", 1)
      println("TEST-2...${result}")
      
      result = driver("abcab", 2)
      println("TEST-3...${result}")
      
      result = driver("488938", 0)
      println("TEST-4...${result}")
      
      result = driver("011010101011", 3)
      println("TEST-5...${result}")
      
      result = driver("aaaa", 2)
      println("TEST-6...${result}")
      
      result = driver("3356203205", 0)
      println("TEST-7...${result}")
      
      result = driver("1010", 2)
      println("TEST-8...${result}")
      
      result = driver("kkXiiTZkGeh", 0)
      println("TEST-9...${result}")
      
          
  }
  
  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;
  }