This is something that Maps are good for.
In a nutshell, you would
- Loop over the accounts being inserted to gather account names into a List or a Set
- Perform a query for Contacts where
Name IN :myAccountNamesCollection
- Use the results of that query to populate a
Map<String, Contact>
where the key of the map (a String) is the name of the contact
- Loop over the accounts being inserted again, and check the map you populated contains a key matching the account name. If it does, then you get the contact from the map, and assign your Account to it
That general gather-query-loop pattern is something you'll encounter and use frequently on the Salesforce platform (and in general, you'll want to operate on collections of records and use loops rather than working with individual records). It's effectively required to be used for us to stay within the governor limits that Salesforce imposes.
One thing to remember with Sets and Maps is that they are one of the few things in Apex that are case-sensitive. Maps and Sets also require exact matches, so using wildcards in your query isn't likely to work well.
Map<String, String> testMap = new Map<String, String>{'Mitchell' => 'good'};
system.debug(testMap.get('Mitchell')); // This prints 'good'
system.debug(testMap.get('mitchell')); // This prints null
system.debug(testMap.get('mitch')); // This prints null
system.debug(testMap.get('Mitch')); // This prints null
system.debug(testMap.get('Mitch%')); // This prints null, the get() method doesn't support wildcards