Skip to content

Commit 1b03663

Browse files
committed
Create naren.java
1 parent 1f0c4c0 commit 1b03663

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

Strings/Fraud Detection/naren.java

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.Hashtable;
5+
import java.util.TreeSet;
6+
import java.util.List;
7+
import java.util.ArrayList;
8+
import java.util.Iterator;
9+
10+
11+
public class Solution {
12+
13+
public static Hashtable<String, List<Record>> fraudsEmail = new Hashtable<String, List<Record>>();
14+
public static Hashtable<String, List<Record>> fraudsAddr = new Hashtable<String, List<Record>>();
15+
public static TreeSet<Integer> fraudsOrder = new TreeSet<Integer>();
16+
17+
public static class Address {
18+
protected String streetAddr;
19+
protected String city;
20+
protected String state;
21+
protected String zip;
22+
}
23+
24+
public static class Record {
25+
protected String orderID;
26+
protected String dealID;
27+
protected String email;
28+
protected Address addr;
29+
protected String card;
30+
}
31+
32+
private static Record createRecord(String[] input) {
33+
Address addr = new Address();
34+
Record rec = new Record();
35+
addr.streetAddr = input[3];
36+
addr.city = input[4];
37+
addr.state = input[5];
38+
addr.zip = input[6];
39+
rec.addr = addr;
40+
rec.orderID = input[0];
41+
rec.dealID = input[1];
42+
rec.email = input[2];
43+
rec.card = input[7];
44+
return rec;
45+
}
46+
47+
private static String parseEmailKey(String email) {
48+
String [] emailInput = email.split("@");
49+
String username = emailInput[0].toLowerCase();
50+
String emailKey = username.contains("+") ? username.split("\\+")[0] : username;
51+
emailKey = emailKey.replace(".", "");
52+
return emailKey;
53+
}
54+
55+
56+
private static void detectFraudEmail(Record rec) {
57+
String emailKey = parseEmailKey(rec.email);
58+
String fraudsEmailKey = emailKey + rec.dealID;
59+
if ( fraudsEmail.containsKey(fraudsEmailKey) ) {
60+
List<Record> recList = fraudsEmail.get(fraudsEmailKey);
61+
for (Record r : recList) {
62+
if ( !rec.card.equals(r.card) ) {
63+
fraudsOrder.add( Integer.parseInt(rec.orderID) );
64+
fraudsOrder.add( Integer.parseInt(r.orderID) );
65+
}
66+
}
67+
recList.add( rec );
68+
} else {
69+
fraudsEmail.put( fraudsEmailKey, new ArrayList<Record> () );
70+
fraudsEmail.get( fraudsEmailKey ).add( rec );
71+
}
72+
}
73+
74+
75+
private static String createAddrKey(Address addr) {
76+
String stAddr = addr.streetAddr.toLowerCase();
77+
String city = addr.city.toLowerCase();
78+
String state = addr.state.toLowerCase();
79+
String zip = addr.zip;
80+
81+
int length = stAddr.length();
82+
String lastTokenStAddr = stAddr.substring( length-3 );
83+
if ( lastTokenStAddr.equals("st.") ) {
84+
stAddr = stAddr.substring( 0, length-3 ) + "street";
85+
} else if ( lastTokenStAddr.equals("rd.") ) {
86+
stAddr = stAddr.substring( 0, length-3 ) + "road";
87+
}
88+
89+
if ( state.equals("new york") ) {
90+
state = "ny";
91+
} else if ( state.equals("california") ) {
92+
state = "ca";
93+
} else if ( state.equals("illinois") ) {
94+
state = "il";
95+
}
96+
return stAddr + city + state + zip;
97+
}
98+
99+
/**
100+
* Function to detect frauds type: same address, same deal id, different
101+
* credit card.
102+
*/
103+
private static void detectFraudAddr(Record rec) {
104+
String addrKey = createAddrKey(rec.addr);
105+
String fraudsAddrKey = addrKey + rec.dealID;
106+
if ( fraudsAddr.containsKey(fraudsAddrKey) ) {
107+
List<Record> recList = fraudsAddr.get( fraudsAddrKey );
108+
for (Record r : recList) {
109+
if ( !rec.card.equals(r.card) ) {
110+
fraudsOrder.add( Integer.parseInt(rec.orderID) );
111+
fraudsOrder.add( Integer.parseInt(r.orderID) );
112+
}
113+
}
114+
recList.add( rec );
115+
} else {
116+
fraudsAddr.put( fraudsAddrKey, new ArrayList<Record> () );
117+
fraudsAddr.get( fraudsAddrKey ).add( rec );
118+
}
119+
}
120+
121+
122+
public static void main(String [] args) throws IOException {
123+
BufferedReader in = new BufferedReader( new InputStreamReader ( System.in ) );
124+
int testCase = Integer.parseInt( in.readLine() );
125+
for (int i = 0; i < testCase; i++) {
126+
String line = in.readLine();
127+
String [] input = line.split(",");
128+
Record rec = createRecord( input );
129+
detectFraudEmail(rec);
130+
detectFraudAddr(rec);
131+
}
132+
133+
String lineToPrint = "";
134+
Iterator<Integer> iter = fraudsOrder.iterator();
135+
while ( iter.hasNext() ) {
136+
lineToPrint += iter.next() + ",";
137+
}
138+
if ( !lineToPrint.equals("") ) {
139+
lineToPrint = lineToPrint.substring( 0, lineToPrint.length()-1);
140+
}
141+
System.out.println( lineToPrint );
142+
143+
}
144+
}

0 commit comments

Comments
 (0)