substring with a O(n) complexity