Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the pattern exists in the text using Java's contains method.
DSA Java
boolean found = text.[1](pattern);Attempts:
3 left
💡 Hint
Common Mistakes
Using indexOf returns an int, not a boolean.
Using equals checks full string equality, not substring presence.
✗ Incorrect
The contains method returns true if the pattern is found anywhere in the text.
2fill in blank
mediumComplete the code to find the first index of the pattern in the text.
DSA Java
int index = text.[1](pattern);Attempts:
3 left
💡 Hint
Common Mistakes
Using contains returns boolean, not index.
Using substring extracts part of the string, not search.
✗ Incorrect
The indexOf method returns the first position where the pattern appears or -1 if not found.
3fill in blank
hardFix the error in the code to correctly check if the pattern starts the text.
DSA Java
boolean starts = text.[1](pattern);Attempts:
3 left
💡 Hint
Common Mistakes
Using contains checks anywhere, not just start.
Using equals checks full string equality.
✗ Incorrect
The startsWith method checks if the text begins with the given pattern.
4fill in blank
hardFill both blanks to extract a substring from text starting at index and ending before endIndex.
DSA Java
String sub = text.[1]([2], endIndex);
Attempts:
3 left
💡 Hint
Common Mistakes
Using length instead of start index.
Using startIndex which is not defined.
✗ Incorrect
substring(start, end) extracts the part of text from start index to endIndex-1. The variable index holds the start position.
5fill in blank
hardFill all three blanks to create a map of pattern lengths to patterns that appear in text with length > 2.
DSA Java
Map<Integer, String> map = new HashMap<>(); for (String pattern : patterns) { if (pattern.[1]() > 2 && text.[2](pattern)) { map.put(pattern.[3](), pattern); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using indexOf instead of contains for substring check.
Using length as a variable instead of method call.
✗ Incorrect
We check pattern length with length(), check if text contains pattern with contains(), and use length() again to put the key in map.
