title: Policemen Catch Thieves
signature: func policemenCatchThieves(arr []rune, n int, k int) int {
test_code: package main
  
  import (
      "fmt"
      "reflect"
      "math"
      "sort"
  )
  
  // SOLUTION CODE
  // ===================================
  PLACEHOLDER_CODE_BODY
  
  // TESTING CODE
  // ===================================
  func validateSolution(actual int, expected int) bool {
      return reflect.DeepEqual(actual,expected)
  }
  
  func driver(arr []rune, n int, k int, expected int) string {
      if validateSolution(PLACEHOLDER_FN_NAME(arr, n, k),expected) {
              return "PASSED"
      }
      return "FAILED"
  
  }
  
  func main() {
  
      // Use imports to avoid undefined errors
      var _ = reflect.DeepEqual
      _ = math.Abs
      _ = sort.Sort
      result := ""
      result = driver([]rune{'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)
      fmt.Printf("TEST-0...%s\n", result)
      
      result = driver([]rune{'7', '6', '0', '1', '0', '1'}, 
  	3, 
  	3, 0)
      fmt.Printf("TEST-1...%s\n", result)
      
      result = driver([]rune{'0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1', '1'}, 
  	9, 
  	10, 0)
      fmt.Printf("TEST-2...%s\n", result)
      
      result = driver([]rune{'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)
      fmt.Printf("TEST-3...%s\n", result)
      
      result = driver([]rune{'1', '1', '5', '8', '8'}, 
  	2, 
  	2, 0)
      fmt.Printf("TEST-4...%s\n", result)
      
      result = driver([]rune{'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)
      fmt.Printf("TEST-5...%s\n", result)
      
      result = driver([]rune{'A', 'I', 'K', 'Q', 'Q', 'X', 'Z', 'f', 'g'}, 
  	5, 
  	8, 0)
      fmt.Printf("TEST-6...%s\n", result)
      
      result = driver([]rune{'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)
      fmt.Printf("TEST-7...%s\n", result)
      
      result = driver([]rune{'0', '0', '0', '1', '1', '1', '1', '1', '1', '1', '1'}, 
  	8, 
  	10, 0)
      fmt.Printf("TEST-8...%s\n", result)
      
      result = driver([]rune{'D', 'C', 'P', 'H', 'G', 'o', 'u', 'P', 'T', 'G', 'E', 'U', 'n', 'E', 'U'}, 
  	11, 
  	12, 1)
      fmt.Printf("TEST-9...%s\n", result)
      
          
  }
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;
  }