Skip to content

Commit 46cb34d

Browse files
authored
Update decrypt-string-from-alphabet-to-integer-mapping.cpp
1 parent 56e7db4 commit 46cb34d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/decrypt-string-from-alphabet-to-integer-mapping.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,27 @@ class Solution2 {
5151
return 'a' + stoi(s) - 1;
5252
}
5353
};
54+
55+
// Time: O(n)
56+
// Space: O(n)
57+
// regex solution
58+
class Solution3 {
59+
public:
60+
string freqAlphabets(string s) {
61+
string result;
62+
int submatches[] = { 1, 2 };
63+
const auto e = regex("(\\d\\d#)|(\\d)");
64+
for (regex_token_iterator<string::const_iterator> it(s.cbegin(), s.cend(), e, submatches), end;
65+
it != end;) {
66+
const auto& a = (it++)->str();
67+
const auto& b = (it++)->str();
68+
result.push_back(alpha(!a.empty() ? a : b));
69+
}
70+
return result;
71+
}
72+
73+
private:
74+
char alpha(const string& s) {
75+
return 'a' + stoi(s) - 1;
76+
}
77+
};

0 commit comments

Comments
 (0)