Skip to content

Commit 9102d8c

Browse files
implemented add account
1 parent b312567 commit 9102d8c

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.thealgorithms.graph;
2+
/**
3+
* for example
4+
Input:
5+
[["abc","abc@mail.com","abx@mail.com"],
6+
["abc","abc@mail.com","aby@mail.com"],
7+
["Mary","mary@mail.com"],
8+
["John","johnnybravo@mail.com"]]
9+
10+
0, 1 Share Name
11+
12+
0, 1 Share email
13+
14+
15+
16+
Output:
17+
[["abc","abc@mail.com","abx@mail.com", "aby@gmail.com"],
18+
["Mary","mary@mail.com"],
19+
["John","johnnybravo@mail.com"]]
20+
21+
Two accounts belong to the same person if they share at least one common email.
22+
Even if two accounts have the same name, they might belong to different people, so merging should only be based on shared emails. Each person can have multiple accounts, and all merged accounts should have the same name.
23+
24+
25+
*
26+
*
27+
*/
28+
import java.util.List;
29+
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.Collections;
32+
public class MergeAccounts {
33+
34+
private static List<List<String>> originalAccounts = new ArrayList<>();
35+
private static List<Integer> Unneeded = new ArrayList<Integer>();
36+
// {
37+
// {"abc", "abc@mail.com", "abx@mail.com"},
38+
// {"abc", "abc@mail.com", "aby@mail.com"},
39+
// {"Mary", "mary@mail.com"},
40+
// {"John", "johnnybravo@mail.com"}
41+
// };
42+
public static void main(String[] args) {
43+
originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "abx@mail.com")));
44+
originalAccounts.add(new ArrayList<>(Arrays.asList("abc", "abc@mail.com", "aby@mail.com")));
45+
originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "mary@mail.com", "many@mail.com")));
46+
originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com")));
47+
originalAccounts.add(new ArrayList<>(Arrays.asList("Mary", "johnnybravo@mail.com","mary@mail.com")));
48+
originalAccounts.add(new ArrayList<>(Arrays.asList("Mohammed", "meedo@mail.com", "Mooda@mail.com")));
49+
50+
51+
System.out.println(originalAccounts);
52+
// originalAccounts = mergeAccounts();
53+
mergeAccounts();
54+
System.out.println(originalAccounts);
55+
}
56+
public static void mergeAccounts(){
57+
for (int account = 0; account < originalAccounts.size(); account++) {
58+
for (int otherAccount = account+1; otherAccount < originalAccounts.size(); otherAccount++) {
59+
if (account != otherAccount){
60+
if(sameNameCheck(account,otherAccount)&&sameEmailCheck(account,otherAccount)){
61+
overlapItems(account, otherAccount);
62+
}
63+
64+
65+
}
66+
}
67+
68+
}
69+
removeUnneeded();
70+
// return originalAccounts;
71+
//
72+
}
73+
74+
public static void removeUnneeded() {
75+
Collections.sort(Unneeded);
76+
System.out.println(Unneeded);
77+
for (int a = Unneeded.size()-1; a > -1 ; a--) {
78+
int i = Unneeded.get(a);
79+
System.out.println(i);
80+
originalAccounts.remove(i);
81+
}
82+
}
83+
public static boolean sameNameCheck(int acc1, int acc2) {
84+
return(originalAccounts.get(acc1).get(0).equals(originalAccounts.get(acc2).get(0)));
85+
}
86+
87+
public static boolean sameEmailCheck(int acc1, int acc2) {
88+
// originalAccounts[acc1] originalAccounts[acc2]
89+
// array of 3
90+
int acc1Size = originalAccounts.get(acc1).size();
91+
int acc2Size = originalAccounts.get(acc2).size();
92+
for (int email = 1; email < acc1Size; email++) {
93+
for (int otherEmail = 1; otherEmail < acc2Size; otherEmail++) {
94+
// System.out.println("Compared "+ originalAccounts[acc1][email]+ " and "+ originalAccounts[acc2][otherEmail]);
95+
if (originalAccounts.get(acc1).get(email).equals(originalAccounts.get(acc2).get(otherEmail)))
96+
return true;
97+
}
98+
}
99+
return false;
100+
101+
}
102+
public static void overlapItems(int acc1, int acc2) {
103+
int acc1Size = originalAccounts.get(acc1).size();
104+
int acc2Size = originalAccounts.get(acc2).size();
105+
if (acc1Size < acc2Size){
106+
int temp = acc1;
107+
acc1 = acc2;
108+
acc2= temp;
109+
temp = acc1Size;
110+
acc1Size = acc2Size;
111+
acc2Size = temp;
112+
}
113+
for (int email = 1; email < acc1Size; email++) {
114+
for (int otherEmail = 1; otherEmail < acc2Size; otherEmail++) {
115+
if (!originalAccounts.get(acc1).contains(originalAccounts.get(acc2).get(otherEmail)))
116+
originalAccounts.get(acc1).add(originalAccounts.get(acc2).get(otherEmail));
117+
118+
}
119+
}
120+
Unneeded.add(acc2);
121+
122+
123+
}
124+
125+
}

0 commit comments

Comments
 (0)