I have implemented a simple pattern matching for the below algorithm in Java with time complexity of O(n)(n is the size of the input array).
Any suggestions for optimization??
one of the most important aspects of accurately matching records to candidates is ensuring that the name in the record matches the name or a known alias of the candidate. In this exercise, you’ll build a method nameMatch() that receives an array of known names and a name from a record to match.
The method should pass the following tests:
1. Exact match
known_aliases = ['Alphonse Gabriel Capone', 'Al Capone']
name_match(known_aliases, 'Alphonse Gabriel Capone') => True
name_match(known_aliases, 'Al Capone') => True
name_match(known_aliases, 'Alphonse Francis Capone') => False
2.Middle name missing (on alias)
known_aliases = ['Alphonse Capone']
name_match(known_aliases, 'Alphonse Gabriel Capone') => True
name_match(known_aliases, 'Alphonse Francis Capone') => True
name_match(known_aliases, 'Alexander Capone') => False
3.Middle name missing (on record name)
known_aliases = ['Alphonse Gabriel Capone']
name_match(known_aliases, 'Alphonse Capone') => True
name_match(known_aliases, 'Alphonse Francis Capone') => False
name_match(known_aliases, 'Alexander Capone') => False
4.More middle name tests
known_aliases = ['Alphonse Gabriel Capone', 'Alphonse Francis Capone']
name_match(known_aliases, 'Alphonse Gabriel Capone') => True
name_match(known_aliases, 'Alphonse Francis Capone') => True
name_match(known_aliases, 'Alphonse Edward Capone') => False
5.Middle initial matches middle name
known_aliases = ['Alphonse Gabriel Capone', 'Alphonse F Capone']
name_match(known_aliases, 'Alphonse G Capone') => True
name_match(known_aliases, 'Alphonse Francis Capone') => True
name_match(known_aliases, 'Alphonse E Capone') => False
name_match(known_aliases, 'Alphonse Edward Capone') => False
name_match(known_aliases, 'Alphonse Gregory Capone') => False
6. Transposition of middle name and first name, All of the test cases implemented previously also apply to the transposed name.
'Gabriel Alphonse Capone' is a valid transposition of 'Alphonse Gabriel Capone'
known_aliases = ['Alphonse Gabriel Capone']
name_match(known_aliases, 'Gabriel Alphonse Capone') => True
name_match(known_aliases, 'Gabriel A Capone') => True
name_match(known_aliases, 'Gabriel Capone') => True
name_match(known_aliases, 'Gabriel Francis Capone') => False
7. Last name cannot be transposed
'Alphonse Capone Gabriel' is NOT a valid transposition of 'Alphonse Gabriel Capone'
'Capone Alphonse Gabriel' is NOT a valid transposition of 'Alphonse Gabriel Capone'
known_aliases = ['Alphonse Gabriel Capone']
name_match(known_aliases, 'Alphonse Capone Gabriel') => False
name_match(known_aliases, 'Capone Alphonse Gabriel') => False
name_match(known_aliases, 'Capone Gabriel') => False
public class PatternMatch {
public boolean nameMatch(List<String> knownAliases, String name){
for(String alias : knownAliases){
if(nameMatch(name,alias)) return true;
}
return false;
}
private boolean nameMatch(String name, String alias){
//case7: lastName check
String aliasLastName = extractLastName(alias);
String lastName = extractLastName(name);
if(aliasLastName.equalsIgnoreCase(lastName)){
String firstName = extractFirstName(name);
String middleName = extractMiddleName(name);
//case6: transposition of middle name and first name, by swapping alias firstName and middleName
if(firstNameAndMiddleNameMatch(firstName,middleName,extractFirstName(alias),extractMiddleName(alias))) return true;
if(firstNameAndMiddleNameMatch(firstName,middleName,extractMiddleName(alias),extractFirstName(alias))) return true;
}
return false;
}
private boolean firstNameAndMiddleNameMatch(String firstName, String middleName, String aliasFirstName, String aliasMiddleName){
if(aliasFirstName == null || aliasFirstName.length() == 0) return false;
boolean firstNameMatch = firstName.equalsIgnoreCase(aliasFirstName);
if(firstNameMatch){
if(middleName!=null && aliasMiddleName!=null){
//case1: exact match
boolean middleNameMatch = middleName.equalsIgnoreCase(aliasMiddleName);
//case5: Middle initial matches middle name
boolean middleInitialsMatch = middleName.charAt(0) == aliasMiddleName.charAt(0);
return middleNameMatch || middleInitialsMatch;
}
//case2 & case3: middle name is missing
return firstNameMatch;
}
return false;
}
private String extractFirstName(String name){
if(name == null || name.length() == 0) return null;
String[] split = name.split(" ");
return split[0];
}
private String extractMiddleName(String name){
if(name == null || name.length() == 0) return null;
String[] split = name.split(" ");
StringBuilder sb = new StringBuilder();
for(int i =1;i<split.length-1;i++){
sb.append(" ");
sb.append(split[i]);
}
if(sb.length() == 0) return null;
return sb.toString().trim();
}
private String extractLastName(String name){
if(name == null || name.length() == 0) return null;
String[] split = name.split(" ");
return split[split.length-1];
}
}