Get Full Names in Java with O(n) Time Complexity


Given two arrays of strings named firstNames and lastNames, return an array containing the full names (separated by one space)

Example:

Input:   firstNames = ["John","Andy","Mary"]
         lastNames = ["Doe","Smith","Johnson"]

Output:  ["John Doe", "Andy Smith", "Mary Johnson"]

Understanding the Problem

The core challenge of this problem is to combine two arrays of strings into a single array of full names. Each full name is formed by concatenating the corresponding elements from the firstNames and lastNames arrays, separated by a space.

This problem is significant in scenarios where you need to merge related data from two different sources. A common application could be in data processing tasks where first and last names are stored separately and need to be combined for display or further processing.

Potential pitfalls include handling arrays of different lengths or empty arrays. However, for this problem, we assume that both arrays are of the same length and non-empty.

Approach

To solve this problem, we can use a straightforward approach:

  1. Initialize an empty array to store the full names.
  2. Iterate through the firstNames and lastNames arrays simultaneously.
  3. For each index, concatenate the first name and last name with a space in between and add the result to the full names array.

This approach is efficient with a time complexity of O(n), where n is the length of the input arrays.

Naive Solution

A naive solution would involve iterating through the arrays and concatenating the strings manually. This is not optimal in terms of readability and maintainability, but it works for small input sizes.

Optimized Solution

The optimized solution follows the same logic but ensures that the code is clean and efficient. We use a single loop to iterate through the arrays and build the full names array.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Create an empty list to store the full names.
  2. Loop through the indices of the firstNames array.
  3. For each index, concatenate the corresponding elements from firstNames and lastNames with a space in between.
  4. Add the concatenated string to the full names list.
  5. Convert the list to an array and return it.

Code Implementation

import java.util.ArrayList;
import java.util.List;

public class FullNames {
    public static String[] getFullNames(String[] firstNames, String[] lastNames) {
        // Initialize a list to store the full names
        List<String> fullNames = new ArrayList<>();
        
        // Iterate through the arrays
        for (int i = 0; i < firstNames.length; i++) {
            // Concatenate first name and last name with a space
            String fullName = firstNames[i] + " " + lastNames[i];
            // Add the full name to the list
            fullNames.add(fullName);
        }
        
        // Convert the list to an array and return it
        return fullNames.toArray(new String[0]);
    }

    public static void main(String[] args) {
        String[] firstNames = {"John", "Andy", "Mary"};
        String[] lastNames = {"Doe", "Smith", "Johnson"};
        
        // Get the full names
        String[] fullNames = getFullNames(firstNames, lastNames);
        
        // Print the full names
        for (String name : fullNames) {
            System.out.println(name);
        }
    }
}

Complexity Analysis

The time complexity of this solution is O(n), where n is the length of the input arrays. This is because we iterate through the arrays once. The space complexity is also O(n) due to the storage of the full names in a new array.

Edge Cases

Potential edge cases include:

Example of handling an empty array:

String[] firstNames = {};
String[] lastNames = {};
String[] fullNames = getFullNames(firstNames, lastNames);
// Expected output: []

Testing

To test the solution comprehensively, consider the following test cases:

Using a testing framework like JUnit can help automate and manage these tests effectively.

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to solve the problem of combining first and last names into full names using Java. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills in programming.

Additional Resources

For further reading and practice, consider the following resources: