-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartE.java
More file actions
104 lines (82 loc) · 2.91 KB
/
PartE.java
File metadata and controls
104 lines (82 loc) · 2.91 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.employee;
import java.util.Arrays;
public class PartE {
public void getFrequencyOfVisitedWebsites(String[] browsingHistory, String[] restrictedWebsites) {
// ************************************ TODO E1 ************************************
/*
* This method should print :
* <<WebsiteName>>:<<Frequency>>
* <<WebsiteName>>:<<Frequency>>
*
* For Example : Instagram:3
* Facebook:0
*
* Pass the frequency in an array in order of the websites.
*
* Uncomment the line "TestYourCode.testFrequencyOfVisitedWebsites(restrictedWebsites,freq)" to test your code
* Note : restrictedWebsites is String array given
* freq is an integer array which contains the frequency of corresponding restrictedWebsites
*/
int freq [] = new int [4] ;
int count = 0;
int k = 0;
for(int i = 0 ; i < restrictedWebsites.length ; i++){
count = 0;
for(int j = 0 ; j < browsingHistory.length ; j++){
if(restrictedWebsites[i].toLowerCase().equals(browsingHistory[j].toLowerCase())){
count++;
freq[k] = count;
}
}
System.out.println(restrictedWebsites[i]+" : "+freq[i]);
k++;
}
// ************************************ SOLUTION E1 BEGIN ************************************
// Uncomment the line below to test your code
TestYourCode.testFrequencyOfVisitedWebsites(restrictedWebsites,freq);
// ************************************ SOLUTION E1 END ************************************
}
public void findSimilarNamedTeams(PermanentEmployee[] perm) {
// ************************************ TODO E2 ************************************
/*
* This method should print number of teams
*
* Uncomment the line "TestYourCode.testSimilarNamedTeam(count)" to test your code
* Note : Here, count is an integer data type storing the number of teams that can be formed.
*/
String[] str = new String[perm.length];
int count = 0;
for(int i =0 ; i< perm.length;i++){
str[i] = perm[i].getName();
}
for(int i =0 ; i< perm.length;i++){
for(int j=i+1 ; j<perm.length ; j++){
if(isAnagram(str[i],str[j]) && str[j] != "0"){
System.out.println(str[i] + " " + str[j]);
count++;
str[j] = "0";
break;
}
}
}
System.out.println(count);
// ************************************ SOLUTION E2 BEGIN ************************************
// Uncomment line below to test your code
TestYourCode.testSimilarNamedTeams(count);
//************************************ SOLUTION E2 END ************************************
}
public boolean isAnagram(String str1, String str2){
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if(str1.equals(str2))
return true;
char[] char1 = str1.toCharArray();
char[] char2 = str2.toCharArray();
Arrays.sort(char1);
Arrays.sort(char2);
for (int i = 0; i < str1.length(); i++)
if (char1[i] != char2[i])
return false;
return true;
}
}