A STRING is a sequence of
characters, such as letters,
numbers, symbols, and spaces. It
is a fundamental data type used
to represent text in
programming.
A string is a sequence of characters. It is used to store and manipulate text.
Key Properties of Strings:
Characters inside quotes: "hello" or 'a'
Immutable in many languages (like C++ std::string and Python)
Indexed: You can access characters by position (starting at index 0)
1. AT BEGINNING - Insert a character at the start of the STRING. All characters shift right by one. i.e., at index 0.
TIME COMPLEXITY - O(n)
2. AT MIDDLE/GIVEN POSITION - Insert a character at a specified position in the STRING. All characters after it shift right. i.e., index = position.
TIME COMPLEXITY - O(n)
3. AT END - Add a character at the end of the STRING. No shifting needed.
TIME COMPLEXITY - O(1)
1. FROM BEGINNING - Remove the first character from the STRING. All characters shift left by one.
TIME COMPLEXITY - O(n)
2. FROM MIDDLE/GIVEN POSITION - Remove a character from a specific index in the STRING. Remaining characters shift left.
TIME COMPLEXITY - O(n)
3. FROM END - Remove the last character from the STRING.
TIME COMPLEXITY - O(1)
Check if a particular character or substring exists within the STRING. This is done by scanning from index 0 to end.
TIME COMPLEXITY - O(n)
Sort the characters of the STRING alphabetically or in reverse order.
TIME COMPLEXITY - O(n²) (using basic sorting like bubble sort)