<p>Take the following string as an example:  </p>

<p>"The quick brown fox"  </p>

<p>Right now the q in quick is at index 4 of the string (starting at 0) and the f in fox is at index 16. Now lets say the user enters some more text into this string.</p>

<p>"The very quick dark brown fox"</p>

<p>Now the q is at index 9 and the f is at index 26.</p>

<p>What is the most efficient method of keeping track of the index of the original q in quick and f in fox no matter how many characters are added by the user?</p>

<p>Language doesn't matter to me, this is more of a theory question than anything so use whatever language you want just try to keep it to generally popular and current languages.</p>

<p>The sample string I gave is short but I'm hoping for a way that can efficiently handle any size string. So updating an array with the offset would work with a short string but will bog down with to many characters. </p>

<p>Even though in the example I was looking for the index of unique characters in the string I also want to be able to track the index of the same character in different locations such as the o in brown and the o in fox. So searching is out of the question.</p>

<p>I was hoping for the answer to be both time and memory efficient but if I had to choose just one I care more about performance speed.</p>

[{'answer_id': 36128, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>Your question is a little ambiguous - are you looking to keep track of the first instances of every letter? If so, an array of length 26 might be the best option.</p>\n\n<p>Whenever you insert text into a string at a position lower than the index you have, just compute the offset based on the length of the inserted string.</p>\n'}, {'answer_id': 36181, 'author': 'Unsliced', 'author_id': 2902, 'author_profile': 'https://Stackoverflow.com/users/2902', 'pm_score': 1, 'selected': False, 'text': '<p>It would also help if you had a target language in mind as not all data structures and interactions are equally efficient and effective in all languages. </p>\n'}, {'answer_id': 177740, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': True, 'text': '<p>Let\'s say that you have a string and some of its letters are <i>interesting</i>. To make things easier let\'s say that the letter at index 0 is always interesting and you never add something before it&mdash;a sentinel. Write down pairs of (interesting letter, distance to the previous interesting letter). If the string is "+the very Quick dark brown Fox" and you are interested in q from \'quick\' and f from \'fox\' then you would write: (+,0), (q,10), (f,17). (The sign + is the sentinel.)</p>\n\n<p>Now you put these in a balanced binary tree whose in-order traversal gives the sequence of letters in the order they appear in the string. You might now recognize the <a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=10&amp;url=http%3A%2F%2Ferikdemaine.org%2Fpapers%2FPartialSums_SODA2004%2Fpaper.ps&amp;ei=jyjrSMPCM4GmQO3FpLQM&amp;usg=AFQjCNGh-7I5N_BA1vpJTGLsmkgcxmOIxg&amp;sig2=AlYsMNNHPxrXNRImTYe9zQ" rel="nofollow noreferrer">partial sums problem</a>: You enhance the tree so that nodes contain (letter, distance, sum). The sum is the sum of all distances in the left subtree. (Therefore sum(x)=distance(left(x))+sum(left(x)).)</p>\n\n<p>You can now query and update this data structure in logarithmic time.</p>\n\n<p>To say that you added <em>n</em> characters to the left of character <em>c</em> you say distance(c)+=n an then go and update sum for all parents of <em>c</em>.</p>\n\n<p>To ask what is the index of <em>c</em> you compute sum(c)+sum(parent(c))+sum(parent(parent(c)))+...</p>\n'}, {'answer_id': 177795, 'author': 'Rafał Dowgird', 'author_id': 12166, 'author_profile': 'https://Stackoverflow.com/users/12166', 'pm_score': 0, 'selected': False, 'text': "<p>The standard trick that usually helps in similar situations is to keep the characters of the string as leaves in a balanced binary tree. Additionally, internal nodes of the tree should keep sets of letters (if the alphabet is small and fixed, they could be bitmaps) that occur in the subtree rooted at a particular node.</p>\n\n<p>Inserting or deleting a letter into this structure only needs O(log(N)) operations (update the bitmaps on the path to root) and finding the first occurence of a letter also takes O(log(N)) operations - you descend from the root, going for the leftmost child whose bitmap contains the interesting letter.</p>\n\n<p>Edit: The internal nodes should also keep number of leaves in the represented subtree, for efficient computation of the letter's index.</p>\n"}]
['https://Stackoverflow.com/questions/36122', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3677/']