Nothing shared by every string can be longer than strs[0], so that string is the first candidate. The outer for visits the other strings; the inner while asks strs[i].StartsWith(prefix, StringComparison.Ordinal) and, on a miss, trims one character with prefix.Substring(0, prefix.Length - 1). The length never goes below zero because every string starts with "", so the trimming stops at the empty candidate at the latest. Pass StringComparison.Ordinal: the one-argument StartsWith(string) is culture-sensitive, slower, and can treat some character sequences as equal when they are not byte-for-byte identical. A C# string is immutable, so each Substring allocates a shorter copy. A first string of m characters that shares nothing with the second costs about m failed checks and m copies, which is where the O(m) space and the m² term come from.