Rope Data Structure - Problem
A Rope is a binary tree data structure for efficiently storing and manipulating very large strings. Each leaf contains a short string and a weight, and each node contains the sum of the weights of all leaves in its left subtree.
Implement a Rope data structure that supports the following operations:
insert(index, text)- Insert text at the given indexdelete(start, end)- Delete characters from start to end (exclusive)charAt(index)- Get character at the given indexsubstring(start, end)- Get substring from start to end (exclusive)length()- Get total length of the rope
For this problem, implement a simplified version that processes a sequence of operations and returns the final string representation.
Input & Output
Example 1 — Basic Insert Operations
$
Input:
operations = [["insert", 0, "hello"], ["insert", 5, " world"]]
›
Output:
"hello world"
💡 Note:
Start with empty rope. Insert "hello" at position 0, then insert " world" at position 5 (end of "hello"). Final string is "hello world".
Example 2 — Mixed Operations
$
Input:
operations = [["insert", 0, "abc"], ["insert", 1, "XY"], ["charAt", 2]]
›
Output:
"Y"
💡 Note:
Insert "abc" at 0 → "abc". Insert "XY" at position 1 → "aXYbc". Get character at index 2 → "Y".
Example 3 — Delete and Length
$
Input:
operations = [["insert", 0, "programming"], ["delete", 3, 7], ["length"]]
›
Output:
"7"
💡 Note:
Insert "programming" → "programming". Delete characters from index 3 to 7 → "proaming" (removed "gr"). Length is 7.
Constraints
- 1 ≤ operations.length ≤ 1000
- 0 ≤ index ≤ current string length
- 1 ≤ text.length ≤ 100
- Text contains only lowercase English letters and spaces
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code