Super Reduced String

Sort by

recency

|

1710 Discussions

|

  • + 0 comments

    Solution using Stack in java:

     public static String superReducedString(String s) {
            String result = "";
            Stack<Character> stack = new Stack<>();
            int n = s.length();
            for(int i = 0; i < n; i++){
                char c = s.charAt(i);
                if(stack.empty() || c != stack.peek())
                    stack.push(c);
                else
                    stack.pop();
                }
            if(stack.empty())
                result = "Empty String";
            
            else
              for(char c: stack)
                  result += c;
            
            return result;        
            }
    
  • + 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
    #include <bits/stdc++.h>
    #include <string>
    using namespace std;
    
    /*
     * Complete the 'superReducedString' function below.
     *
     * The function is expected to return a STRING.
     * The function accepts STRING s as parameter.
     */
    
    string superReducedString(string s) {
        for(int i=1;i<s.length();i++){
            if(s[i]==s[i-1]){
                s.erase(s.begin()+i);
                s.erase((s.begin()+(i-1)));
            i=1;
                
            }
            if(s.length()==0){
                return "Empty String";
            }
            
        }
        if(s[0]==s[1]){
            return "Empty String";
        }
        return s;
    }
    
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string s;
        getline(cin, s);
    
        string result = superReducedString(s);
    
        fout << result << "\n";
    
        fout.close();
    
        return 0;
    }
    
  • + 0 comments

    SWIFT

    func logic(charCount: Int, strings: [UInt8], string: UInt8) -> [UInt8] {
        var newStrings: [UInt8] = strings
        
        if charCount % 2 == 1 {
            if newStrings.last ?? 0 == string { newStrings.popLast() }
            else { newStrings.append(string) }
        }
    
        return newStrings
    }
    
    func superReducedString(s: String) -> String {
        // Write your code here
        var oldStrings: [UInt8] = [UInt8](s.utf8)
        var newStrings: [UInt8] = [UInt8]()
        
        var strTemp: UInt8 = oldStrings.first!
        var charCount: Int = 0
            
        for index in 0 ... oldStrings.count - 1 {
            if oldStrings[index] == strTemp {
              charCount += 1
              continue
            }
            
            newStrings = logic(charCount: charCount, strings: newStrings, string: oldStrings[index - charCount])
            strTemp = oldStrings[index]
            charCount = 1
        }
            
        newStrings = logic(charCount: charCount, strings: newStrings, string: oldStrings[oldStrings.count - 1])
        
        return newStrings.count == 0 ? "Empty String" : String(bytes: newStrings, encoding: String.Encoding.utf8) ?? ""
    }
    
  • + 0 comments

    c++ solution without erasing or adding stuff to data structures - using previous left and right (pl and pr) indexes and an array to store elements that have been "erased" (used vector).

    string superReducedString(string s) { int n = s.size(); vector used(n,false); int pl = -1, pr = -1;

    int i = 0;
    while (i < n) {
        if (i+1 < n and s[i]==s[i+1]) {
            int l = !used[i] ? i : pl;
            int r = !used[i+1] ? i+1 : pr;
    
            while (l >= 0 and r < n) {
                if (s[l]!=s[r]) break;
    
                used[l]=used[r]=true;
    
                while (l>=0 and used[l]) l--;
                while (r<n and used[r]) r++;
            }
            pl = l, pr = r;
        }
        i=max(i+1,pr);
    }