Skip to content

Commit 72b881e

Browse files
committed
✨ hashmap and linear search
1 parent c8454d1 commit 72b881e

18 files changed

+856
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build output.
6+
build/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Big Time complexity - O(1)
2+
Space Complexity - O(n)
3+
4+
[Algorithm Explanation](https://stackoverflow.com/questions/63369213/is-the-extra-space-complexity-of-a-function-that-counts-letter-occurrences-o1)
5+
6+
[Algorithm Explanation](https://stackoverflow.com/questions/27334494/space-complexity-of-hashmap-when-iterating-over-an-array-in-linear-time)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file configures the static analysis results for your project (errors,
2+
# warnings, and lints).
3+
#
4+
# This enables the 'recommended' set of lints from `package:lints`.
5+
# This set helps identify many issues that may lead to problems when running
6+
# or consuming Dart code, and enforces writing Dart using a single, idiomatic
7+
# style and format.
8+
#
9+
# If you want a smaller set of lints you can change this to specify
10+
# 'package:lints/core.yaml'. These are just the most critical lints
11+
# (the recommended set includes the core lints).
12+
# The core lints are also what is used by pub.dev for scoring packages.
13+
14+
include: package:lints/recommended.yaml
15+
16+
# Uncomment the following section to specify additional rules.
17+
18+
# linter:
19+
# rules:
20+
# - camel_case_types
21+
22+
# analyzer:
23+
# exclude:
24+
# - path/to/excluded/files/**
25+
26+
# For more information about the core and recommended set of lints, see
27+
# https://dart.dev/go/core-lints
28+
29+
# For additional information about configuring this file, see
30+
# https://dart.dev/guides/language/analysis-options
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:count_occurance_of_a_word/count_occurance_of_a_word.dart'
2+
as hashmap;
3+
4+
void main(List<String> arguments) {
5+
String sentence = "The cat ate the dog";
6+
Map<String, int> results = hashmap.hashmap(sentence);
7+
print("results $results");
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'dart:collection';
2+
3+
/***
4+
* The purpose is to use hashmap and count the number of times a word appears in a phrase also called word frequency
5+
* for example:
6+
* 1. hello world::
7+
* hello: 1
8+
* world: 1
9+
* 2. the cat ate the dog
10+
* the: 2
11+
* cat: 1
12+
* ate: 1
13+
* dog: 1
14+
*
15+
* Using HashMap to store the words and count the numbers
16+
* first we need to tokenize the phrase
17+
*
18+
* method should take the sentence and return a hashmap
19+
*/
20+
21+
Map<String, int> hashmap(String sentence) {
22+
final Map<String, int> map = HashMap();
23+
List<String> tokenize = sentence.split(" ");
24+
for (int i = 0; i < tokenize.length; i++) {
25+
String word = tokenize[i].toLowerCase();
26+
print("word $word");
27+
if (map.containsKey(word)) {
28+
int count = map[tokenize[i]] ?? 0;
29+
var data = <String, int>{word: count + 1};
30+
map.addAll(data);
31+
} else {
32+
var data = <String, int>{word: 1};
33+
map.addAll(data);
34+
}
35+
}
36+
return map;
37+
}

0 commit comments

Comments
 (0)