Super Reduced String

Sort by

recency

|

1722 Discussions

|

  • + 0 comments

    RUST:

    fn superReducedString(s: &str) -> String {
        let mut last_idx = s.len() as i32;
        let mut result: Vec<&str> = s.split("").collect();
        let mut flag = true;
    
        while flag {
            flag = false;
            for n in 0..last_idx {
                if n <= last_idx {
                    let first = n as usize;
                    if result[first] == result[(n+1) as usize] {
                        result.remove(first);
                        result.remove(first);
                        last_idx -=2;
                        flag = true;
                    } 
                }   
            }
        }
        if !(result.join("").is_empty()) {
            return result.join("")
        }
        "Empty String".to_string()
    }
    
  • + 0 comments

    Best Approche Cpp

    string superReducedString(string s) { stackstack;

    for(char ch : s){
        if(!stack.empty()){
            if(stack.top() != ch){
                stack.push(ch);
            }else{
                stack.pop();
            }
        }else{
            stack.push(ch);
        }
    }
    
    if(stack.empty()){
        return  "Empty String";
    }
    
    string ans = "";
    
    while (!stack.empty()) {
        ans.push_back(stack.top());
        stack.pop();
    }
    
    reverse(ans.begin() , ans.end());
    
    return  ans;
    

    }

  • + 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 is my Python solution!

    def superReducedString(s):
        s = list(s)
        change = True
        while change:
            change = False
            for letter in range(len(s) - 1):
                if letter < len(s) - 1:
                    if s[letter] == s[letter + 1]:
                        change = True
                        s.pop(letter)
                        s.pop(letter)
        if s:
            return "".join(s)
        return "Empty String"
    
  • + 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