Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
string
#include<iostream> #include<string> #include<algorithm> #include<unordered_set> #include<vector> using namespace std; bool isPalindrome(const string& s) { int i, j; for(i=0, j=s.size()-1; i<j; ++i, --j) { if(s[i]!=s[j]) break; } if(i<j) return false; else return true; } void removeDupER(string& s) { for(string::iterator it = s.begin(); it!=s.end(); it++) { char c = *it; s.erase(remove(it+1, s.end(), c), s.end()); } } string removeDupSTL(string& s) { unordered_set<char> se; string str; for(auto& c : s) { if(se.find(c)==se.end()) { se.insert(c); str.push_back(c); } } return str; } void removeDupInplace(string& s) { sort(s.begin(), s.end()); int i=0,j=0; while(i<s.size()) { int j = i+1; while(j<s.size() && s[i]==s[j]) { s[j]='$'; j++; } i=j; } cout << s << endl; i = 0; j = 1; while(j<s.size()) { while(j<s.size() && s[j]=='$') j++; if(j<s.size()) { i++; s[i] = s[j]; j++; } } s.resize(i+1); } int main() { string s("ahgfndgcsaaa"); string t = s; cout << s << endl << endl; cout << "Checking if palindrome.." << isPalindrome(s) << endl; cout << "Removing duplicates from string using erase_remove..."; removeDupER(s); cout << s << endl; s = t; cout << "Removing duplicates from string using STL containers..."; s = removeDupSTL(s); cout << s << endl; s = t; cout << "Removing duplicates from string in-place using sorting..."; removeDupInplace(s); cout << s << endl; s = t; sort(s.begin(), s.end()); cout << "After sorting..." << s << endl; /* cout << "Remove duplicates from sorted string non-STL..."; removeDup(s); cout << s << endl; */ }
run
|
edit
|
history
|
help
0
Microsoft - # of fragments (semi-optimised)
Biggest even palindrom
find-missing-number-arithmetic-progression
void sun()
member function pointer
543
Dar
team name
Day3
queue