Super Reduced String

Sort by

recency

|

1719 Discussions

|

  • + 0 comments

    solution in python

    def superReducedString(s): # Write your code here pat=r'(.)\1' while True: rs=re.sub(pat,'',s) if s==rs: break s=rs return 'Empty String' if len(s)==0 else s

  • + 0 comments

    def superReducedString(s): # Write your code here pat=r'(.)\1' while True: rs=re.sub(pat,'',s) if s==rs: break s=rs return 'Empty String' if len(s)==0 else s

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/XgJKCkb1EjQ

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main()
    {
        string s;
        cin >> s;
        int i = 1;
        while(i < s.size()){
            if(s[i-1] == s[i]){
                s.erase(i-1, 2);
                if( i != 1) i--;
            }
            else i++;
        }
        if(s == "") s = "Empty String";
        cout << s;
        return 0;
    }
    
  • + 0 comments

    Here's my solution in typescript. Strings are immutable in JS and I don't think creating an array is efficient using String.prototype.split('') and then again converting it into a string using Array.prototype.join([ ])

    function superReducedString(s: string): string {
        const [reducedString, cannotBeReducedFurther] = reduceString(s);
        if (cannotBeReducedFurther) {
            return reducedString || 'Empty String';
        }
        return superReducedString(reducedString);
    }
    
    function reduceString(s: string): [string, boolean] {
        let idx = 0;
        let result = '';
        let noAdjacentCharactersFound = true;
        while (idx < s.length) {
            if (s.charAt(idx) === s.charAt(idx + 1)) {
                idx += 2;
                noAdjacentCharactersFound = false;
            } else {
                result += s.charAt(idx++);
            }
        }
        return [result, noAdjacentCharactersFound];
    }
    
  • + 0 comments

    Perl regex desicion:

    sub superReducedString {
        my $s = shift;
        
        while ($s =~ m/(.)\1/) {
            $s =~ s/(.)\1//;
        }
        return ($s) ? $s : "Empty String";
    }