Q: String manipulation in Python Recently I gave a test on HackerRank and my question is:
Give a string return the most concise string that can be formed from it for example:
string = 'watson  Represents|watson represents|Watson represents a first step into cognitive systems, a new era of computing.|first step into  Cognitive|Cognitive Systems; a new era|what does watson represent'



*

*The following string contains many duplicates like Watson represents we have to ignore extra spacing between characters or lower/upper case.watson  Represents watson represents are same thing.

*Semicolon and comma represent the same thing. For example Cognitive Systems; a new era is present inside Watson represents a first step into cognitive systems, a new era of computing. 

*Your final string should not contain any duplicates ignore lowercase/uppercase or extra space if you have 


My Answer :
watson = 'watson  Represents|watson represents|Watson represents a first step into cognitive systems, a new era of computing.|first step into  Cognitive|Cognitive Systems; a new era|what does watson represent'

import re

watson = re.sub(r';', r',', watson)  #replace the semicolon with colon
watson = watson.strip().split('|')
removeWatson = list(watson)

for i in range(len(watson)):

    for j in range(len(watson)):

        if j == i:
            continue

        if " ".join(watson[i].strip().lower().split()) in " ".join(watson[j].strip().lower().split()):
            removeWatson[i] = ''

print "|".join(filter(None, removeWatson))

My answer is definitely inefficient and I am wondering if you can suggest me to alternative way to solve this problem.
Most concise string is : Watson represents a first step into cognitive systems, a new era of computing.|what does watson represent

A: string = 'watson  Represents|watson represents|Watson represents a first   step into cognitive systems, a new era of computing.|first step into  Cognitive|Cognitive Systems; a new era|what does watson represent'
ll=string.split("|")
ll.sort(key=len)
import re
ll2=[re.sub(r"\s+"," ",re.sub(r"[;,]+","",i.lower())) for i in ll]
j=1
k=0
for i in ll2:
    if re.findall(r"\b"+i.lower()+r"\b","|".join(ll2[j:]),flags=re.I):
        string=string.replace(ll[k],"",1)
    k=k+1
    j=j+1
print re.sub(r"^\|+|\|(?=\|)|\|+$","",string

You can do it in 1 loop using re.

A: My thinking is that I am being asked to represent the original string exactly, i.e. I can reproduce the original from my most concise version.
In other words - compress it.
from __future__ import print_function
from zlib import compress, decompress

string = 'watson  Represents|watson represents|Watson represents a first step into cognitive systems, a new era of computing.|first step into  Cognitive|Cognitive Systems; a new era|what does watson represent'

print("Original length:", len(string))
compressed = compress(string)
print("Compressed length:", len(compressed))
decompressed = decompress(compressed)
print("Decompressed is equal:", decompressed == string)

Result is:
Original length: 198
Compressed length: 116
Decompressed is equal: True

