Advertisement
neunmalelf

Bitap algorithm

Dec 8th, 2024
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 0.73 KB | Software | 0 0
  1. function bitapSearch(text: string, pattern: string): string | null {
  2.     const m = pattern.length;
  3.  
  4.     if (m === 0) {
  5.         return text;
  6.     }
  7.  
  8.     // Initialize the bit array R.
  9.     const R: boolean[] = new Array(m + 1).fill(false);
  10.     R[0] = true;
  11.  
  12.     for (let i = 0; i < text.length; i++) {
  13.         // Update the bit array.
  14.         for (let k = m; k >= 1; k--) {
  15.             R[k] = R[k - 1] && (text[i] === pattern[k - 1]);
  16.         }
  17.  
  18.         if (R[m]) {
  19.             return text.substring(i - m + 1, i + 1);
  20.         }
  21.     }
  22.  
  23.     return null;
  24. }
  25.  
  26. // Example usage:
  27. const text = "this is a simple example";
  28. const pattern = "simple";
  29. const result = bitapSearch(text, pattern);
  30. console.log(result); // Output: "simple"
Tags: typescript
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement