title: Policemen Catch Thieves
signature: policemenCatchThieves :: [Char] -> Integer -> Integer -> Integer
  policemenCatchThieves arr n k = 
test_code: import Text.Printf
  import Control.Exception (catch, SomeException)
  import Data.Typeable
  import qualified Data.List as List
  import qualified Data.Map as Map
  import qualified Data.Set as Set
  
  -- SOLUTION CODE
  -- ============================================
  
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  validateSolution :: Integer -> Integer -> Bool
  validateSolution actual expected = do
      actual == expected
  
  
  driver :: [Char] -> Integer -> Integer -> Integer -> String
  driver arr n k expected = do
      if (validateSolution (PLACEHOLDER_FN_NAME arr n k) expected)
          then "PASSED"
      else "FAILED"
  
  main = do
      printf "TEST-0...%s\n" (driver (['A', 'B', 'B', 'D', 'E', 'E', 'F', 'G', 'G', 'G', 'I', 'J', 'O', 'P', 'Q', 'Q', 'Q', 'Q', 'R', 'R', 'S', 'U', 'X', 'Y', 'Y', 'c', 'd', 'h', 'i', 'i', 'i', 'i', 'k', 'k', 'l', 'l', 'l', 'l', 'm', 'p', 'r', 'r', 's', 't', 't', 'u', 'x', 'z']) (33) (45) (0) )
  
      printf "TEST-1...%s\n" (driver (['7', '6', '0', '1', '0', '1']) (3) (3) (0) )
  
      printf "TEST-2...%s\n" (driver (['0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1']) (9) (10) (0) )
  
      printf "TEST-3...%s\n" (driver (['y', 'k', 'S', 'i', 's', 'r', 'i', 'z', 'y', 'f', 'E', 'U', 'y', 'G', 'f', 'f', 's', 'v', 'v', 'D', 'v', 'V', 'S', 'D', 'K', 'S', 'f', 'V', 'g', 'I', 'J', 'p', 'j', 'k', 'R', 'n', 'm', 'O', 'L', 'X', 'y', 'U', 'y', 'k', 'w']) (24) (44) (0) )
  
      printf "TEST-4...%s\n" (driver (['1', '1', '5', '8', '8']) (2) (2) (0) )
  
      printf "TEST-5...%s\n" (driver (['0', '1', '0', '0', '1', '1', '1', '0', '0', '0', '1', '0', '1', '0', '1', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '1', '1', '1', '0', '0', '0', '0', '0', '0']) (23) (18) (0) )
  
      printf "TEST-6...%s\n" (driver (['A', 'I', 'K', 'Q', 'Q', 'X', 'Z', 'f', 'g']) (5) (8) (0) )
  
      printf "TEST-7...%s\n" (driver (['7', '0', '6', '9', '7', '5', '1', '3', '9', '8', '0', '0', '1', '3', '9', '2', '5', '5', '2', '7', '9', '3', '3', '9', '3', '8', '5', '5', '0', '4', '6', '2', '7', '4', '0', '4', '6', '4', '2', '3']) (35) (28) (0) )
  
      printf "TEST-8...%s\n" (driver (['0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1']) (8) (10) (0) )
  
      printf "TEST-9...%s\n" (driver (['D', 'C', 'P', 'H', 'G', 'o', 'u', 'P', 'T', 'G', 'E', 'U', 'n', 'E', 'U']) (11) (12) (1) )
  
source_py: def policemen_catch_thieves(arr, n, k):
      i = 0
      l = 0
      r = 0
      res = 0
      thi = []
      pol = []
      while (i < n):
          if (arr[i] == 'P'):
              pol.append(i)
          elif (arr[i] == 'T'):
              thi.append(i)
          i += 1
      while ((l < len(thi)) and (r < len(pol))):
          if (abs((thi[l] - pol[r])) <= k):
              res += 1
              l += 1
              r += 1
          elif (thi[l] < pol[r]):
              l += 1
          else:
              r += 1
      return res
source_cpp: int policemen_catch_thieves(vector<char> arr, int n, int k) {
    int res = 0;
    vector < int > thi;
    vector < int > pol;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( arr [ i ] == 'P' ) pol . push_back ( i );
      else if ( arr [ i ] == 'T' ) thi . push_back ( i );
    }
    int l = 0, r = 0;
    while ( l < thi . size ( ) && r < pol . size ( ) ) {
      if ( abs ( thi [ l ] - pol [ r ] ) <= k ) {
        res ++;
        l ++;
        r ++;
      }
      else if ( thi [ l ] < pol [ r ] ) l ++;
      else r ++;
    }
    return res;
  }