Search code examples
phpstringduplicateschars

How to remove repeated chars from string but with exceptions like 'good' or 'cool'?


I'm trying to remove repeated chars from strings like I looooovvee this. It's awesomee. Very gooood.

to an output like: I love this. It's awesome, Very good.

I'm already using in PHP this instrunction $str=preg_replace("/(.)\1+/", "$1", $str);

But it outputs I love this. It's awesome. Very god.

The problem is in words that already should have repeated chars like 'good' or 'cool'


Solution

  • I suppose you could store your allowed words (like "good" and "cool") in a Trie Dictionary.

    Whenever you are check a word for duplicate chars, you should allow duplicate characters upto the point where the Dictionary still have some valid words with that prefix.

    When the dictionary has no valid words for the prefix, you can remove the duplicate chars from that point on

    Eg: if the word you are checking is "Goooood"

    check "Go" in trie it will return "God" and "Good" as valid words
    check "Goo" in trie, it will return "Good" as the valid word
    check "Gooo" in trie, it will say there are no valid words

    Therefore you keep upto "Goo" and remove the rest of the o's