Strings in DSA: Why Immutability and Character Codes Matter for Coding Problems
A string is stored as a contiguous array of characters, so reading its length or any character by index takes constant time. Each character is also a numeric code, and subtracting a run's first code, such as 'a' or '0', turns a letter or digit into a usable array position. In Java, Python and JavaScript strings are immutable, so repeatedly appending inside a loop copies the whole string each time and becomes quadratic in cost. Using a StringBuilder, a list joined once, or an array joined once avoids that slowdown. The article's guidance is that most string problems reduce to familiar array techniques such as counting, two pointers and sliding windows.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in