-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRunTimePractice.java
72 lines (64 loc) · 2.65 KB
/
RunTimePractice.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.HashMap;
public class RunTimePractice {
public static int findNumberOfRepetitions(String s, char c) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
sum++;
}
}
return sum;
}
// Brute force solution
public static int[] findNumberOfRepetitionsv1(String s, char[] c) {
int[] sums = new int[c.length]; // 1 time
for (int i = 0 ; i < s.length(); i++) { // 1 time, length of string + 1, set number of times
for (int j = 0; j < c.length; j++) {
if (s.charAt(i) == c[j]) {
sums[j] = sums[j] + 1;
}
}
}
return sums;
}
public static int[] findNumberOfRepetitionsv2(String s, char[] c) {
// Optimal time: 0(n+m)
int[] sums = new int[c.length]; // 1 time
HashMap<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (!map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), 1);
} else {
int sum = map.get(s.charAt(i));
map.put(s.charAt(i), sum+1);
}
}
for (int j = 0; j < c.length; j++) {
int sum;
if (!map.containsKey(c[j])) {
sums[j] = 0;
} else {
sums[j] = map.get(c[j]);
}
}
return sums;
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
System.out.println(findNumberOfRepetitions("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", 'a'));
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;
System.out.println("Test: " + duration + "ms");
char[] a = {'a', 'b'};
startTime = System.currentTimeMillis();
System.out.println(findNumberOfRepetitionsv1("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", a));
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("Test: " + duration + "ms");
startTime = System.currentTimeMillis();
System.out.println(findNumberOfRepetitionsv2("abcadasgiashdgsiangpiangwpaoewapoyweapnpghoinapoieapiyapiaepwiygnhepwaiynhapiygnhjewpaojepaojygpaoejyapoejypoyjahgpoaj", a));
endTime = System.currentTimeMillis();
duration = endTime - startTime;
System.out.println("Test: " + duration + "ms");
}
}