We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Strings
- Super Reduced String
- Discussions
Super Reduced String
Super Reduced String
+ 0 comments JavaScript Solution:
function superReducedString(s) { const splittedString = s.split(''); for (var i = 0; i <= splittedString.length - 1; i++) { if (splittedString[i] === splittedString[i + 1]) { splittedString.splice(i, 2); i = -1; }; } return splittedString.length ? splittedString.join('') : 'Empty String'; }
+ 1 comment public static String superReducedString(String s) { // Write your code here Stack<Character> stack = new Stack<>(); for (int i = 0; i < s.length(); i++) { Character ch = s.charAt(i); if (!stack.isEmpty() && ch == stack.peek()) { stack.pop(); } else { stack.push(ch); } } /* Print final result */ StringBuilder result = new StringBuilder(); while(!stack.isEmpty()){ result.insert(0,stack.pop()); System.out.println(result); } if(result.length()==0){ return "Empty String"; } return result.toString(); }
+ 0 comments **Java Solution **
- public static String superReducedString(String s) {
- String str = "";
- int a ;
- for(int i = 0;i
- if(s.length()>1 && s.charAt(i) == s.charAt(i+1)){
- s = s.substring(0,i) + s.substring(i+2);
- a = -i;
- } else{
- a = 1;
- }
- }
- str = (s.length() == 0) ? "Empty String" : s;
- return str;
- }
+ 0 comments Python Solution
def superReducedString(s): if len(s) < 2: return s i,n = 0,len(s) while i < n - 1: if s[i] == s[i+1]: s = s[:i] + s[i+2:] i,n = 0,len(s) continue i += 1 return "Empty String" if n == 0 else s
+ 0 comments Python solution
def superReducedString(s): i = 0 s = list(s) while i < len(s)-1: if s[i]== s[i+1]: del s[i] del s[i] if i != 0: i -= 1 # to cover baab situation else: i += 1 return (''.join(s)) if len(s) != 0 else "Empty String"
Load more conversations
Sort 1625 Discussions, By:
Please Login in order to post a comment