HahsMap
key => toLowerCase, trim()
// validate the input
if (book == null || book.length == 0 || word == null)
return -1;
"book" , 2
"description", 2
"date", 3
"is", 3
// validate the input
if (book == null || book.length == 0 || word == null)
2. Implementation
return -1;
str = str.toLowerCase();
if ( str.trim() != "" )
{
str = str.toLowerCase();
if ( str.trim() != "" )
{
2. Implementation
// Solution: Single query // Time:O(n) Count the number of times a word appears
public int getWordFrequency (String[] book, String word) { // validate the input if (book == null || book.length == 0 || word == null) return -1; HAshMap3. Similar Onesmap = wordFrequency(book); word = word.toLowerCase; if ( map.containsKey(word) ) { return map.get(word); } return 0; } //public void WordFrequencyTable(String[] book) public HashMap wordFrequency(String[] book) { HashMap map = new HashMap<>(); // validate the input if (book == null || book.length == 0) return map; for (int i =0 ; i< book.length;i++) { String str = book[i]; str = str.toLowerCase(); if ( str.trim() != "" ) { if ( !map.containsKey(str) ) map.put(str,1); else map.put(str, map.get(str)+1); } } return map; }
No comments:
Post a Comment