Discover how computers find your words instantly in huge texts without reading every letter!
Why Substring Search Patterns in DSA Javascript?
Imagine you have a huge book and you want to find every place where a certain word appears. Doing this by reading every page and checking each word one by one is tiring and slow.
Manually scanning through text to find a word means checking each letter one by one. This takes a lot of time, especially if the book is very long. It's easy to miss matches or get confused, making mistakes common.
Substring search patterns use smart ways to quickly find where a word appears inside a bigger text. They skip unnecessary checks and jump ahead, saving time and avoiding errors.
function findWord(text, word) {
for (let i = 0; i <= text.length - word.length; i++) {
let found = true;
for (let j = 0; j < word.length; j++) {
if (text[i + j] !== word[j]) {
found = false;
break;
}
}
if (found) return i;
}
return -1;
}function kmpSearch(text, word) {
// build prefix table and use it to skip checks
// ... implementation ...
return -1;
}It enables fast and reliable searching of words or patterns inside large texts, making tasks like spell checking, data filtering, and searching much quicker.
Search engines use substring search patterns to quickly find your search words inside billions of web pages, giving you results in seconds.
Manual search is slow and error-prone for large texts.
Substring search patterns speed up finding words by skipping unnecessary checks.
This makes searching in big data fast and reliable.
Practice
indexOf method do when used on a string in JavaScript?Solution
Step 1: Understand the purpose of
TheindexOfindexOfmethod searches a string for a specified substring and returns the position of its first occurrence.Step 2: Confirm behavior on strings
If the substring is found, it returns the zero-based index; if not found, it returns -1.Final Answer:
Finds the position of the first occurrence of a substring -> Option DQuick Check:
indexOf= position of substring [OK]
- Confusing indexOf with replace method
- Thinking indexOf splits the string
- Assuming indexOf changes the string case
'cat' exists in the string text using includes method?Solution
Step 1: Recall the correct method name
The correct method to check substring presence isincludes, called on the string object.Step 2: Verify syntax
The syntax isstring.includes(substring), sotext.includes('cat')is correct.Final Answer:
text.includes('cat') -> Option CQuick Check:
Correct method call syntax = text.includes('cat') [OK]
string.includes(substring) to check presence [OK]- Using wrong method name like include or contains
- Calling includes as a standalone function
- Passing arguments in wrong order
const text = 'hello world';
const pos = text.indexOf('o');
console.log(pos);Solution
Step 1: Identify the string and substring
The string is 'hello world' and the substring searched is 'o'.Step 2: Find the first occurrence of 'o'
Counting from zero: h(0) e(1) l(2) l(3) o(4) (space 5) w(6) o(7). The first 'o' is at index 4.Step 3: Verify the output
The code usesindexOf('o')which returns the first occurrence index, which is 4.Final Answer:
4 -> Option AQuick Check:
First 'o' index = 4 [OK]
- Counting index starting from 1 instead of 0
- Confusing second 'o' position with first
- Assuming indexOf returns -1 if substring exists
'test' exists in str. What is the error?const str = 'this is a test';
if (str.indexOf == 'test') {
console.log('Found');
} else {
console.log('Not Found');
}Solution
Step 1: Analyze the use of indexOf
The code usesstr.indexOfwithout parentheses, so it refers to the function itself, not the result of calling it.Step 2: Understand the comparison
Comparing a function to a string ('test') is always false, so the condition is incorrect.Final Answer:
indexOf is used without parentheses, so it compares function to string -> Option BQuick Check:
Missing parentheses on indexOf causes error [OK]
- Forgetting parentheses on indexOf
- Using wrong comparison operator
- Assuming indexOf returns boolean directly
'ana' in the string 'banana'. Which approach correctly finds all positions?Solution
Step 1: Understand the problem of multiple occurrences
To find all positions, you must repeatedly search starting after the last found position.Step 2: Use a loop with indexOf and update start
UseindexOf('ana', start)in a loop, updatingstartto position + 1 each time until no more found.Final Answer:
Use a loop withindexOfand update the start position after each find -> Option AQuick Check:
Loop + indexOf with updated start = all positions [OK]
- Using includes which returns only true/false
- Using lastIndexOf which finds only last occurrence
- Using replace which changes string and loses positions
Ready to prove it?
Pass the test and this pattern moves unknown
