title: Longest Prefix Also Suffix 1
signature: pub fn longest_prefix_also_suffix_1(s: String) -> i32 {
test_code: use std::collections::HashMap;
  use std::collections::HashSet;
  use std::cmp::*;
  use std::collections::*;
  use std::fmt::*; 
  
  
  // SOLUTION CODE
  // ============================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE 
  // ============================================
  pub fn validate_solution(actual: i32, expected: i32)->bool{
  return actual==expected
  }
  pub fn driver(s: String, expected: i32)->String{
      if validate_solution(PLACEHOLDER_FN_NAME(s), expected) {
        return "PASSED".to_string();
      };
      return "FAILED".to_string();
  
  }
  
  fn main() {
      println!("TEST-0...{}", driver(
        "aabcdaabc".to_string(), 
        4
      ));
      
      println!("TEST-1...{}", driver(
        "1372494598".to_string(), 
        0
      ));
      
      println!("TEST-2...{}", driver(
        "110000100001".to_string(), 
        1
      ));
      
      println!("TEST-3...{}", driver(
        "abcab".to_string(), 
        2
      ));
      
      println!("TEST-4...{}", driver(
        "488938".to_string(), 
        0
      ));
      
      println!("TEST-5...{}", driver(
        "011010101011".to_string(), 
        3
      ));
      
      println!("TEST-6...{}", driver(
        "aaaa".to_string(), 
        2
      ));
      
      println!("TEST-7...{}", driver(
        "3356203205".to_string(), 
        0
      ));
      
      println!("TEST-8...{}", driver(
        "1010".to_string(), 
        2
      ));
      
      println!("TEST-9...{}", driver(
        "kkXiiTZkGeh".to_string(), 
        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;
  }