Start learning free

LESSON 150 OF 409 · LOOPING OVER ARRAY INDICES

Get Full Names in C++ (Time Complexity: O(n))

Practice Read the full explanation

Given two vectors of strings, the first names and the last names (given in this order, called firstNames and lastNames in the example), return a vector 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"]
STEP 1 OF 5
    Ctrl + Enter to check

    C++ · LOOPING OVER ARRAY INDICES

    Get Full Names in C++, explained

    Understanding the Problem

    The core challenge of this problem is to combine two arrays of strings into a single array of full names. This is a common task in data processing where you need to merge first and last names into a single string for each individual.

    Potential pitfalls include ensuring that both arrays are of the same length and handling any empty strings appropriately.

    Approach

    To solve this problem, we can use a simple loop to iterate through both arrays simultaneously and concatenate the corresponding elements from each array with a space in between.

    Here is a step-by-step approach:

    1. Initialize an empty array to store the full names.
    2. Iterate through the indices of the input arrays.
    3. For each index, concatenate the first name and last name with a space in between.
    4. Append the concatenated string to the result array.
    5. Return the result array.

    Algorithm

    Let's break down the algorithm:

    1. Initialize an empty vector fullNames.
    2. Loop through the indices of the firstNames array.
    3. For each index i, concatenate firstNames[i] and lastNames[i] with a space in between.
    4. Push the concatenated string into the fullNames vector.
    5. Return the fullNames vector.

    Code Implementation

    
    #include <vector>
    #include <string>
    #include <iostream>
    
    std::vector<std::string> getFullNames(const std::vector<std::string>& firstNames, const std::vector<std::string>& lastNames) {
        // Initialize an empty vector to store full names
        std::vector<std::string> fullNames;
        
        // Ensure both arrays are of the same length
        if (firstNames.size() != lastNames.size()) {
            throw std::invalid_argument("Input arrays must have the same length");
        }
        
        // Iterate through the arrays and concatenate first and last names
        for (size_t i = 0; i < firstNames.size(); ++i) {
            fullNames.push_back(firstNames[i] + " " + lastNames[i]);
        }
        
        return fullNames;
    }
    
    int main() {
        std::vector<std::string> firstNames = {"John", "Andy", "Mary"};
        std::vector<std::string> lastNames = {"Doe", "Smith", "Johnson"};
        
        std::vector<std::string> fullNames = getFullNames(firstNames, lastNames);
        
        for (const auto& name : fullNames) {
            std::cout << name << std::endl;
        }
        
        return 0;
    }
    

    Complexity Analysis

    The time complexity of this approach is O(n), where n is the number of elements in the input arrays. This is because we are iterating through the arrays once. The space complexity is also O(n) due to the storage required for the output array.

    Edge Cases

    Potential edge cases include:

    • Empty input arrays: The function should return an empty array.
    • Arrays of different lengths: The function should throw an exception or handle the error gracefully.
    • Empty strings within the arrays: The function should still concatenate the strings correctly.

    Testing

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

    • Standard case with non-empty arrays.
    • Empty input arrays.
    • Arrays with empty strings.
    • Arrays of different lengths (should throw an exception).

    Thinking and Problem-Solving Tips

    When approaching such problems, it's important to:

    • Understand the problem requirements and constraints.
    • Break down the problem into smaller, manageable steps.
    • Consider edge cases and how to handle them.
    • Write clean, readable code with comments to explain your logic.

    Conclusion

    In this blog post, we discussed how to merge two arrays of strings into a single array of full names. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for data processing tasks and helps improve problem-solving skills.

    Additional Resources

    For further reading and practice, consider the following resources:

    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free