Given a string "this is high-tech job market in which? we make. careers" I have to remove all special characters except hyphen and count number of words in a string so output should be 10 in this case. I have written below program but it did not pass the test cases.
public int countWords(String str) {
if(str.isEmpty() || str==null)
return 0;
String replacedString = str.replaceAll(["^a-zA-Z0-9- ]","");
String[] arrWords = replacedString.split("\\s+");
return arrWords.length;
}