Skip to content

Commit

Permalink
Update decrypt-string-from-alphabet-to-integer-mapping.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Jan 5, 2020
1 parent 56e7db4 commit 46cb34d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions C++/decrypt-string-from-alphabet-to-integer-mapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,27 @@ class Solution2 {
return 'a' + stoi(s) - 1;
}
};

// Time: O(n)
// Space: O(n)
// regex solution
class Solution3 {
public:
string freqAlphabets(string s) {
string result;
int submatches[] = { 1, 2 };
const auto e = regex("(\\d\\d#)|(\\d)");
for (regex_token_iterator<string::const_iterator> it(s.cbegin(), s.cend(), e, submatches), end;
it != end;) {
const auto& a = (it++)->str();
const auto& b = (it++)->str();
result.push_back(alpha(!a.empty() ? a : b));
}
return result;
}

private:
char alpha(const string& s) {
return 'a' + stoi(s) - 1;
}
};

0 comments on commit 46cb34d

Please sign in to comment.