Optimize character ranges without using excessive memory