What if you could find any phrase in a huge book instantly without reading every word?
Why Substring Search Patterns in DSA Java?
Imagine you have a huge book and you want to find every place where a certain phrase appears. Doing this by reading every word and checking manually would take forever!
Manually scanning through text is slow and easy to mess up. You might miss some matches or waste time checking the same parts again and again.
Substring search patterns use smart ways to quickly find where a smaller text appears inside a bigger one, skipping unnecessary checks and saving lots of time.
for (int i = 0; i <= text.length() - pattern.length(); i++) { boolean found = true; for (int j = 0; j < pattern.length(); j++) { if (text.charAt(i + j) != pattern.charAt(j)) { found = false; break; } } if (found) System.out.println("Found at " + i); }
int index = text.indexOf(pattern); while (index >= 0) { System.out.println("Found at " + index); index = text.indexOf(pattern, index + 1); }
It lets you quickly find all occurrences of a phrase in large texts without checking every single character again and again.
Search engines use substring search patterns to find your search words inside billions of web pages almost instantly.
Manual search is slow and error-prone.
Substring search patterns speed up finding text inside text.
They help software like search engines work fast and accurately.
Practice
indexOf method in Java's String class return when searching for a substring that exists in the string?Solution
Step 1: Understand the purpose of
TheindexOfindexOfmethod searches for the first occurrence of a substring inside a string and returns its position.Step 2: Identify the return value when substring exists
If the substring is found,indexOfreturns the starting index (0-based) of that substring in the string.Final Answer:
The starting index of the first occurrence of the substring -> Option AQuick Check:
indexOfreturns index, not count or boolean [OK]
- Thinking indexOf returns count of occurrences
- Assuming it returns true/false
- Confusing indexOf with substring length
"cat" in string str in Java?Solution
Step 1: Recall Java String methods for substring search
Java'sStringclass usesindexOfto find substring positions.Step 2: Check method names and return types
findandsearchare not valid String methods;containsreturns boolean, not int.Final Answer:
int pos = str.indexOf("cat"); -> Option CQuick Check:
UseindexOffor substring position [OK]
- Using non-existent methods like find or search
- Using contains which returns boolean, not index
- Confusing method names from other languages
String text = "hello world";
int pos = text.indexOf("lo");
System.out.println(pos);Solution
Step 1: Locate substring "lo" in "hello world"
Counting indices: h(0), e(1), l(2), l(3), o(4). The substring "lo" starts at index 3 (l at 3, o at 4).Step 2: Understand indexOf return value
indexOfreturns the starting index of the substring, which is 3 here.Final Answer:
3 -> Option BQuick Check:
Substring "lo" starts at index 3 [OK]
- Counting from 1 instead of 0
- Returning index of second character in substring
- Assuming -1 means substring found
String s = "banana";
int index = s.indexOf('an');
System.out.println(index);Solution
Step 1: Check substring argument type in indexOf
TheindexOfmethod expects aStringargument, but single quotes denote achar.Step 2: Identify correct syntax for substring
Substring literals must be in double quotes, so 'an' should be "an".Final Answer:
Using single quotes for substring instead of double quotes -> Option AQuick Check:
Substring literals need double quotes [OK]
- Using single quotes for multi-character substrings
- Confusing char and String types
- Ignoring compiler error messages
text, which code snippet correctly finds all starting indices of substring "ab" in text using indexOf?Solution
Step 1: Understand how to find multiple occurrences with indexOf
UseindexOf(substring, fromIndex)to search from a moving start index to find all occurrences.Step 2: Analyze each option's logic
int index = 0; while ((index = text.indexOf("ab", index)) != -1) { System.out.print(index + " "); index++; } correctly updates index after each found occurrence to avoid infinite loop and find all positions. Others either do not update index properly or repeatedly search from start.Final Answer:
int index = 0; while ((index = text.indexOf("ab", index)) != -1) { System.out.print(index + " "); index++; } -> Option DQuick Check:
Use indexOf with start index and increment after each find [OK]
- Not updating start index causing infinite loop
- Always searching from start index 0
- Confusing indexOf usage with for-loop index
Ready to prove it?
Pass the test and this pattern moves unknown
