Problems - Set 1
Easy
Word Match
Approach 1: Using string split (One time)
package algo;
public class Problem1 {
public static void main(String[] args) {
String bookText = "Java is great. Java is powerful. Java is everywhere!";
String word = "Java";
int freq = getWordFrequency(bookText, word); // Output: 3
System.out.println(freq);
}
public static int getWordFrequency(String bookText, String word) {
if (bookText == null || word == null || word.isEmpty()) {
return 0;
}
String[] words = bookText.toLowerCase().split("\\W+"); // split on non-word characters
int count = 0;
for (String w : words) {
if (w.equals(word.toLowerCase())) {
count++;
}
}
return count;
}
}Approach 2: Using StringUtils method
Approach 3: Optimized for Multiple Calls
Medium
Pattern Matching
Example
Constraints
Approach
Arithmetic Evaluation
Operator Precedence (BODMAS):
Last updated