Super Reduced String

Sort by

recency

|

44 Discussions

|

  • + 0 comments

    !/bin/python3

    def superReducedString(s): # Write your code here stack = [] for char in s: if stack and stack[-1] == char: stack.pop() else: stack.append(char) reduced_string = ''.join(stack) return reduced_string if reduced_string else 'Empty String'

    if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()
    
    result = superReducedString(s)
    
    fptr.write(result + '\n')
    
    fptr.close()
    
  • + 0 comments

    My answer in Python

    def superReducedString(s):
        print(s)
        pattern = r"(.)\1"
        
        while re.search(pattern, s):
            s = re.sub(pattern, "", s)
            print(s)
        
        if s:
            return s
        else:
            return "Empty String"
    
  • + 0 comments
    public static string superReducedString(string s)
    {
        if(s.Length == 1) return s;        
        var i=1;
        while(i < s.Length){
            if(s.Substring(i-1, 1) == s.Substring(i, 1)){
                s = s.Remove(i-1, 2);
                if(i>1) i--;
            }
            else {
                i++;
            }
        }
        return s == "" ? "Empty String" : s;
    }
    
  • + 0 comments

    Only three lines: Some practices in Hackerrank seem more difficult than easy and somes seem easier than intermediate.

    def superReducedString(s): # Write your code here

    for letter in s:
        s = s.replace(2*letter,"")
    
    return s if len(s) != 0 else "Empty String"
    
  • + 0 comments

    Java O(n)

    public static String superReducedString(String s) {
            Stack<Character> stack = new Stack<>();
            
            for (char c : s.toCharArray()) {
                if (!stack.isEmpty() && stack.peek() == c) {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
            
            StringBuilder sb = new StringBuilder();
            for (char c : stack) {
                sb.append(c);
            }
            
            return sb.length() == 0 ? "Empty String" : sb.toString();
        }