Problem
- Given a list of strings our function should encode the list into a single string using some type of delimiter to the start and end of the next string.
Example
Approach 1
- We know that while encoding we need some type of delimiter to denote the start and end of a string. If we use something like
#
to separate the wordsneet
,code
into a single stringneet#code
, this would work, however it does not cover the edge case where a string can have a#
sign in the middle of the string such asco#de
. To prevent this we can use the length of the original string as sort of a double proof. Using the length of the string our encoded string should look like4#neet5#co#de
. Although we have two#
signs in the second string we know that the first#
sign we find will have an accompanying integer to represent where our first string ends. Using the length we can extract the 4 characters from the first string and know that second string will have 5 characters including the#
sign.
Todo
- Encode string by concatenating
Length of word + # + word
- Create a list to hold words after decoding
- Begin loop to iterate over each character
- Create a second pointer to keep track of
#
sign- While second pointer != #, increase index
- Iterate from index i to j to extract the integer
- Move i index right after
#
sign and movej
index to end of the word- Append the word from index
[i:j]
- Return result
Implementation
Complexity Analysis
- Time Complexity: m is the sum of all strings and n is the number of stirngs
- Space Complexity: