We have a list of Person objects, identify all duplicate persons according to the following rules:
Two persons are considered the same if they have the same last name and the same first name.
Two first names are considered the same if they are literally the same or they appear in the same row of the provided dictionary (the DICTIONARY variable on the top of the file).
Each row of the dictionary String (DICTIONARY) contains comma separated strings representing first names that are considered equivalent.
Example:
bob, robert
liz, elizabeth
A Person can have a first name that is not included in the DICTIONARY, which means the name has no other equivalent values.
Complete the following:
import java.io.*;
import java.util.*;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang3.builder.EqualsBuilder;
import java.util.stream.Collectors;
class Solution{
private static final String DICTIONARY = "bob, robert\nliz, elizabeth\nben, benjamin\nnate, nathan, nathaniel\nandy, andrew\nchris, christopher\nalex, alexander";
public static List<List<Person>> duplicates(List<Person> person){
}
/**
* The Person class
*/
public static class Person {
private String firstName;
private String lastName;
public Person (String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return this.firstName;
}
public String getLastName() {
return this.lastName;
}
@Override
public String toString() {
return String.format("%s %s", this.firstName, this.lastName);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Person that = (Person) o;
return new EqualsBuilder()
.append(firstName, that.firstName)
.append(lastName, that.lastName)
.build();
}
@Override
public int hashCode() {
return Objects.hash(firstName, lastName);
}
}
}Sample Input:
List<Person> input = Arrays.asList(
new Person("bob", "jones"),
new Person("noel", "smith"),
new Person("elizabeth", "smith"),
new Person("robert", "jones"),
new Person("noel", "smith")
);
Expected Output:
Arrays.asList(
Arrays.asList(new Person("noel", "smith"), new Person("noel", "smith")),
Arrays.asList(new Person("bob", "jones"), new Person("robert", "jones"))
);I can think of O(n^2) solution but can't get less time complexity solution?