Strings•Easy
Substring Extraction
Substring Extraction

A Substring is a contiguous sequence of characters contained within a larger parent string. For example, "gram" is a substring of "Programming". Extracting a substring is a key operation in text processing, parsing, and lexical analysis.
Implementation Mechanics
- In Static/Low-level Languages (like C): Substring extraction requires allocating a target buffer (with enough capacity plus 1 extra byte for the null-terminator), then copying characters one-by-one from the source array starting at index
startup tostart + length, and manually assigning the null-terminator'\0'at the end. - In Object-Oriented Languages (like C++): The standard library provides the
.substr(start, length)method, which handles memory management and returns a newstd::stringobject. - In Managed/Immutable Systems (like Java/JavaScript): Methods like
.substring(beginIndex, endIndex)are available. In older Java runtimes, substrings shared the parent string's character array to save memory, which could lead to memory leaks if a tiny substring kept a huge parent string in memory. Modern runtimes create a clean new copy to avoid this.
Complexity Analysis
- Time Complexity:
- Best/Worst/Average Case:
O(K)(whereKis the length of the extracted substring, since we must copy exactlyKcharacters)
- Best/Worst/Average Case:
- Space Complexity:
O(K)(to store the extracted substring in memory)