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 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 C++ solution
std::list<char> super_reduced_list(std::list<char> &l) { int k = l.size(); for(auto it1 = l.begin(), it2 = ++l.begin(); (it1 != l.end()) && (it2 != l.end()); ) { if(*it1 == *it2) { it2 = l.erase(it1); it1 = l.erase(it2); it2 = it1; it2++; } else { it1++; it2++; } } for(char c : l) { std::cout << c; } std::cout << std::endl; if(k == l.size()) { return l; } else { return super_reduced_list(l); } } string superReducedString(string s) { std::list<char> l(s.begin(), s.end()); super_reduced_list(l); return (l.empty())? "Empty String" : string(l.begin(), l.end()); }
+ 0 comments Java solution:
public static String superReducedString(String s) { while (s != (s = s.replaceAll("([a-z])\\1{1,1}", ""))){} return s.isEmpty()? "Empty String" : s; }
+ 0 comments My code is running everywhere and giving right output but while running test cases like **"aa" and "baab" ** fails?
public static String superReducedString(String s) { // Write your code here StringBuilder sb = new StringBuilder(s+""); int l = 0; while(l<=sb.length()-2){ if(sb.charAt(l)==sb.charAt(l+1)){ String temp = superReducedString(sb.delete(l, l+2).toString()); s = temp; // return sb.toString(); } l++; } if(s=="") return "Empty string"; return s; }
+ 0 comments C# solution
public static string superReducedString(string s) { string d,e; for(int i=0;i<s.Length-1;i++) { d= s.Substring(i, 1); e = s.Substring(i+1, 1); if (e == d) { s=s.Remove(i, 2); if (i == 0) i = i - 1; else i = i - 2; } } if (s.Length<1) { s="Empty String"; } return s; }
Load more conversations
Sort 1554 Discussions, By:
Please Login in order to post a comment