Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the length of the pattern string.
DSA Java
int m = pattern.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using size() which is for collections, not strings.
Using getLength() which does not exist for strings.
✗ Incorrect
The length() method returns the number of characters in a string in Java.
2fill in blank
mediumComplete the code to check if the substring of text matches the pattern.
DSA Java
if (text.substring(i, i + m).[1](pattern)) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using contains() which checks if pattern is anywhere inside, not exact match.
Using equalsIgnoreCase() which ignores case differences.
✗ Incorrect
equals() checks if two strings have exactly the same characters in the same order.
3fill in blank
hardFix the error in the loop condition to avoid index out of bounds.
DSA Java
for (int i = 0; i <= text.length() - [1]; i++) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using pattern.length() + 1 causes missing last possible match.
Using pattern.length() - 1 causes substring to go beyond text length.
✗ Incorrect
We stop at text length minus pattern length to avoid substring going out of bounds.
4fill in blank
hardFill both blanks to correctly count pattern matches in the text.
DSA Java
int count = 0; for (int i = 0; i <= text.length() - [1]; i++) { if (text.substring(i, i + [2]).equals(pattern)) { count++; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using text.length() in substring causes errors.
Using pattern.length() - 1 misses last character.
✗ Incorrect
Both blanks must be pattern.length() to correctly check substrings of the pattern's length.
5fill in blank
hardFill all three blanks to return the first index of pattern in text or -1 if not found.
DSA Java
for (int i = 0; i <= text.length() - [1]; i++) { if (text.substring(i, i + [2]).equals([3])) { return i; } } return -1;
Attempts:
3 left
💡 Hint
Common Mistakes
Using text.length() in substring or loop causes errors.
Comparing substring to text instead of pattern.
✗ Incorrect
We use pattern.length() for loop limit and substring length, and compare substring to pattern.
