Strings in DSA: Immutability, Character Codes and Patterns
First published on CodeKairo , where the figures in this lesson are animated step by step. In short: A string is a sequence of characters stored like an array, so its length and the character at any index are O(1) to read. In Java, Python and JavaScript strings are immutable: every change creates a new string, so appending in a loop costs O(n²) unless you use a StringBuilder or join a list. Most…
A string is composed of characters stored sequentially, similar to an array. Its length and the character at any position can be accessed in constant time, O(1). In Java, Python and JavaScript, strings are immutable, meaning any operation that appears to modify a string actually generates a new string. This behavior can lead to inefficient O(n²) performance when appending characters in a loop, unless a StringBuilder or list join is used.
String problems often involve counting characters, using two pointers, or applying a sliding window technique. As strings are essentially arrays of characters, understanding this fundamental structure is crucial for solving string-based problems efficiently.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to