D
D
Dimash Kenzhegaliev2021-07-13 09:35:02
JavaScript
Dimash Kenzhegaliev, 2021-07-13 09:35:02

How to ideally measure the distance between certain characters in a string in a strict order?

i have 2 strings they can be anything,
i get an array of arrays
where in each nested array are the indexes of each character from the second string to the first string.
the number of these indices is n, since the first line can contain several identical characters with different indices, I need to get an array of character indices in strict order and with the smallest distance between these characters, that is, for example, I have the following first line:
'apple iphone smartphone 11 256gb purple mhdu3'
second line:
'iphone'
this gives an array like this:
[ [ 17 ], [ 10, 11, 18, 33, 36 ], [ 19, 41 ], [ 20 ], [ 21 ], [ 13 , 22, 38 ] ]
as a result of processing it, I want to get this
[ 17, 18, 19, 20, 21, 22 ],
accordingly, if the index of some character does not fit into a strict order, that is, if there is such a situation:
[ [17], [11, 9], 18 ]
then you need to catch this moment and return false
please help, otherwise I have already been 3 days old I'm working on it, nothing works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2021-07-13
@Giranda22-Git

We take the first element of the first array and go through the rest of the arrays, choosing the first of the elements of the next array, which is greater than the one selected at the previous stage. If this is not found - an error. Why work here for three days?
In general, the solution, IMHO, smacks of overengineering.
What prevents you from finding a solution in a simple cycle "we are looking for the first position of the next character in the substring from the previous one found to the end of the string"? How the hell do you want to sort them all out?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question