Encode and Decode Strings - Leetcode 271

preview_player
Показать описание


#leetcode #neetcode #python
Рекомендации по теме
Комментарии
Автор

Or use an escape character as most strings in programming languages do.

YuvrajRaghuvanshiS
Автор

A software we're using picked a character in the Unicode Private Use Areas, and uses it to separate parts in a string. It's actually pretty clever, since that area is guaranteed by Unicode never to be assigned, and thus allows interested parties to use their for own purposes.

cevaggelou
Автор

A trick I've used for when you want to load an entire line of a file into a single field in a database is to use the hex character for backspace (\08 I think) because you can almost guarantee there will never be a backspace character in your data (unless it's some sort of user behavior logging data). I imagine you could use that as a delimiter here although the interviewer might not be happy with it :)

AntonioDoesMetal
Автор

I've failed a C++ interview on this. Was an interval mapping question but the gist of the solution was similar to this.

louisuchihatm
Автор

Yeah, this is used in binary packet structures, well packets of bytes. Then you add in the fact you have bytes that represent start of message, end of message, and maybe a CRC check section, and what happens if the message portion contains a byte that is also the SOM byte value, you need to introduce stuffing where any byte greater than say 250 is split into 2 bytes with the remainder over 250 in the second byte. Now encode and decide that. Fun times. You get to a point where you look at these streams of bytes for so long, you can read is encoded.

PaulPendor
Автор

Serialized data in vBulletin did work a bit like that: was resembling JSON without being JSON

simonebernacchia
Автор

Do you even need the spectator # if you already saved the length of each string?

reiskoryphae
Автор

so then what if the number# pattern already exists in the list of strings?

deemon
Автор

Seems a bit or an overkill... Just use an uncommon unicode character as separator, or first escape your separator

ronsijm
Автор

I think when I had a problem like that I just used \0

Smileyassassinc
Автор

Can you just use a number instead of number-special character? Looks like it can be omitted and it will work the same

ljubisaknezevic
Автор

Woww, i always used to do that in my mind for various questions, didn't knew this was a pattern also. Nice

sarvarthmonga
Автор

i used | and it worked, but this is a better solution

sumkid
Автор

Dude sounds like a character from Silicon Valley show. Not surprisingly though.

aniketbisht
Автор

what if original words contains number and #?
4#neet5#night, cat, dog

kekwait
Автор

I just joined all the strings with my own name as the delimiter, and split them using the same delimiter - hehe

sarthak.khanduja
Автор

This is also essentially how Redis's Resp serialisation works

Ciano
Автор

Prepend string with metadata. You have info you need and it displays as normal string.

RukopisyNarnie
Автор

this is also quite similar to bencodes

surplusvalue
Автор

just hope you don't have a number to encode at the end of your string, encoding an address for example like this is bound to have issue. You could use invisible characters or better combinaison of characters (like #a trigram of the software / your name / the company#).
Remember that this type of problem you have to ask question to define what are the constraint and type of data, and explain why you would implement this and not something else.

Ryuhikuro