Skip to content

Commit 312c0d9

Browse files
committed
feat: add solutions to lc problem: No.2227
No.2227.Encrypt and Decrypt Strings
1 parent 3fda942 commit 312c0d9

File tree

6 files changed

+405
-0
lines changed

6 files changed

+405
-0
lines changed

solution/2200-2299/2227.Encrypt and Decrypt Strings/README.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,161 @@ encrypter.decrypt("eizfeiam"); // return 2.
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76+
**方法一:哈希表**
77+
7678
<!-- tabs:start -->
7779

7880
### **Python3**
7981

8082
<!-- 这里可写当前语言的特殊实现逻辑 -->
8183

8284
```python
85+
class Encrypter:
86+
87+
def __init__(self, keys: List[str], values: List[str], dictionary: List[str]):
88+
self.mp = dict(zip(keys, values))
89+
self.cnt = Counter(self.encrypt(v) for v in dictionary)
90+
91+
def encrypt(self, word1: str) -> str:
92+
res = []
93+
for c in word1:
94+
if c not in self.mp:
95+
return ''
96+
res.append(self.mp[c])
97+
return ''.join(res)
98+
99+
def decrypt(self, word2: str) -> int:
100+
return self.cnt[word2]
83101

102+
103+
# Your Encrypter object will be instantiated and called as such:
104+
# obj = Encrypter(keys, values, dictionary)
105+
# param_1 = obj.encrypt(word1)
106+
# param_2 = obj.decrypt(word2)
84107
```
85108

86109
### **Java**
87110

88111
<!-- 这里可写当前语言的特殊实现逻辑 -->
89112

90113
```java
114+
class Encrypter {
115+
private Map<Character, String> mp = new HashMap<>();
116+
private Map<String, Integer> cnt = new HashMap<>();
117+
118+
public Encrypter(char[] keys, String[] values, String[] dictionary) {
119+
for (int i = 0; i < keys.length; ++i) {
120+
mp.put(keys[i], values[i]);
121+
}
122+
for (String w : dictionary) {
123+
w = encrypt(w);
124+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
125+
}
126+
}
127+
128+
public String encrypt(String word1) {
129+
StringBuilder sb = new StringBuilder();
130+
for (char c : word1.toCharArray()) {
131+
if (!mp.containsKey(c)) {
132+
return "";
133+
}
134+
sb.append(mp.get(c));
135+
}
136+
return sb.toString();
137+
}
138+
139+
public int decrypt(String word2) {
140+
return cnt.getOrDefault(word2, 0);
141+
}
142+
}
143+
144+
/**
145+
* Your Encrypter object will be instantiated and called as such:
146+
* Encrypter obj = new Encrypter(keys, values, dictionary);
147+
* String param_1 = obj.encrypt(word1);
148+
* int param_2 = obj.decrypt(word2);
149+
*/
150+
```
151+
152+
### **C++**
153+
154+
```cpp
155+
class Encrypter {
156+
public:
157+
unordered_map<string, int> cnt;
158+
unordered_map<char, string> mp;
159+
160+
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
161+
for (int i = 0; i < keys.size(); ++i) mp[keys[i]] = values[i];
162+
for (auto v : dictionary) cnt[encrypt(v)]++;
163+
}
164+
165+
string encrypt(string word1) {
166+
string res = "";
167+
for (char c : word1)
168+
{
169+
if (!mp.count(c)) return "";
170+
res += mp[c];
171+
}
172+
return res;
173+
}
174+
175+
int decrypt(string word2) {
176+
return cnt[word2];
177+
}
178+
};
179+
180+
/**
181+
* Your Encrypter object will be instantiated and called as such:
182+
* Encrypter* obj = new Encrypter(keys, values, dictionary);
183+
* string param_1 = obj->encrypt(word1);
184+
* int param_2 = obj->decrypt(word2);
185+
*/
186+
```
91187
188+
### **Go**
189+
190+
```go
191+
type Encrypter struct {
192+
mp map[byte]string
193+
cnt map[string]int
194+
}
195+
196+
func Constructor(keys []byte, values []string, dictionary []string) Encrypter {
197+
mp := map[byte]string{}
198+
cnt := map[string]int{}
199+
for i, k := range keys {
200+
mp[k] = values[i]
201+
}
202+
e := Encrypter{mp, cnt}
203+
for _, v := range dictionary {
204+
e.cnt[e.Encrypt(v)]++
205+
}
206+
return e
207+
}
208+
209+
func (this *Encrypter) Encrypt(word1 string) string {
210+
var ans strings.Builder
211+
for _, c := range word1 {
212+
if v, ok := this.mp[byte(c)]; ok {
213+
ans.WriteString(v)
214+
} else {
215+
return ""
216+
}
217+
}
218+
return ans.String()
219+
}
220+
221+
func (this *Encrypter) Decrypt(word2 string) int {
222+
return this.cnt[word2]
223+
}
224+
225+
/**
226+
* Your Encrypter object will be instantiated and called as such:
227+
* obj := Constructor(keys, values, dictionary);
228+
* param_1 := obj.Encrypt(word1);
229+
* param_2 := obj.Decrypt(word2);
230+
*/
92231
```
93232

94233
### **TypeScript**

solution/2200-2299/2227.Encrypt and Decrypt Strings/README_EN.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,150 @@ encrypter.decrypt(&quot;eizfeiam&quot;); // return 2.
7474
### **Python3**
7575

7676
```python
77+
class Encrypter:
7778

79+
def __init__(self, keys: List[str], values: List[str], dictionary: List[str]):
80+
self.mp = dict(zip(keys, values))
81+
self.cnt = Counter(self.encrypt(v) for v in dictionary)
82+
83+
def encrypt(self, word1: str) -> str:
84+
res = []
85+
for c in word1:
86+
if c not in self.mp:
87+
return ''
88+
res.append(self.mp[c])
89+
return ''.join(res)
90+
91+
def decrypt(self, word2: str) -> int:
92+
return self.cnt[word2]
93+
94+
95+
# Your Encrypter object will be instantiated and called as such:
96+
# obj = Encrypter(keys, values, dictionary)
97+
# param_1 = obj.encrypt(word1)
98+
# param_2 = obj.decrypt(word2)
7899
```
79100

80101
### **Java**
81102

82103
```java
104+
class Encrypter {
105+
private Map<Character, String> mp = new HashMap<>();
106+
private Map<String, Integer> cnt = new HashMap<>();
107+
108+
public Encrypter(char[] keys, String[] values, String[] dictionary) {
109+
for (int i = 0; i < keys.length; ++i) {
110+
mp.put(keys[i], values[i]);
111+
}
112+
for (String w : dictionary) {
113+
w = encrypt(w);
114+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
115+
}
116+
}
117+
118+
public String encrypt(String word1) {
119+
StringBuilder sb = new StringBuilder();
120+
for (char c : word1.toCharArray()) {
121+
if (!mp.containsKey(c)) {
122+
return "";
123+
}
124+
sb.append(mp.get(c));
125+
}
126+
return sb.toString();
127+
}
128+
129+
public int decrypt(String word2) {
130+
return cnt.getOrDefault(word2, 0);
131+
}
132+
}
133+
134+
/**
135+
* Your Encrypter object will be instantiated and called as such:
136+
* Encrypter obj = new Encrypter(keys, values, dictionary);
137+
* String param_1 = obj.encrypt(word1);
138+
* int param_2 = obj.decrypt(word2);
139+
*/
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
class Encrypter {
146+
public:
147+
unordered_map<string, int> cnt;
148+
unordered_map<char, string> mp;
149+
150+
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
151+
for (int i = 0; i < keys.size(); ++i) mp[keys[i]] = values[i];
152+
for (auto v : dictionary) cnt[encrypt(v)]++;
153+
}
154+
155+
string encrypt(string word1) {
156+
string res = "";
157+
for (char c : word1)
158+
{
159+
if (!mp.count(c)) return "";
160+
res += mp[c];
161+
}
162+
return res;
163+
}
164+
165+
int decrypt(string word2) {
166+
return cnt[word2];
167+
}
168+
};
169+
170+
/**
171+
* Your Encrypter object will be instantiated and called as such:
172+
* Encrypter* obj = new Encrypter(keys, values, dictionary);
173+
* string param_1 = obj->encrypt(word1);
174+
* int param_2 = obj->decrypt(word2);
175+
*/
176+
```
83177
178+
### **Go**
179+
180+
```go
181+
type Encrypter struct {
182+
mp map[byte]string
183+
cnt map[string]int
184+
}
185+
186+
func Constructor(keys []byte, values []string, dictionary []string) Encrypter {
187+
mp := map[byte]string{}
188+
cnt := map[string]int{}
189+
for i, k := range keys {
190+
mp[k] = values[i]
191+
}
192+
e := Encrypter{mp, cnt}
193+
for _, v := range dictionary {
194+
e.cnt[e.Encrypt(v)]++
195+
}
196+
return e
197+
}
198+
199+
func (this *Encrypter) Encrypt(word1 string) string {
200+
var ans strings.Builder
201+
for _, c := range word1 {
202+
if v, ok := this.mp[byte(c)]; ok {
203+
ans.WriteString(v)
204+
} else {
205+
return ""
206+
}
207+
}
208+
return ans.String()
209+
}
210+
211+
func (this *Encrypter) Decrypt(word2 string) int {
212+
return this.cnt[word2]
213+
}
214+
215+
/**
216+
* Your Encrypter object will be instantiated and called as such:
217+
* obj := Constructor(keys, values, dictionary);
218+
* param_1 := obj.Encrypt(word1);
219+
* param_2 := obj.Decrypt(word2);
220+
*/
84221
```
85222

86223
### **TypeScript**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Encrypter {
2+
public:
3+
unordered_map<string, int> cnt;
4+
unordered_map<char, string> mp;
5+
6+
Encrypter(vector<char>& keys, vector<string>& values, vector<string>& dictionary) {
7+
for (int i = 0; i < keys.size(); ++i) mp[keys[i]] = values[i];
8+
for (auto v : dictionary) cnt[encrypt(v)]++;
9+
}
10+
11+
string encrypt(string word1) {
12+
string res = "";
13+
for (char c : word1)
14+
{
15+
if (!mp.count(c)) return "";
16+
res += mp[c];
17+
}
18+
return res;
19+
}
20+
21+
int decrypt(string word2) {
22+
return cnt[word2];
23+
}
24+
};
25+
26+
/**
27+
* Your Encrypter object will be instantiated and called as such:
28+
* Encrypter* obj = new Encrypter(keys, values, dictionary);
29+
* string param_1 = obj->encrypt(word1);
30+
* int param_2 = obj->decrypt(word2);
31+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
type Encrypter struct {
2+
mp map[byte]string
3+
cnt map[string]int
4+
}
5+
6+
func Constructor(keys []byte, values []string, dictionary []string) Encrypter {
7+
mp := map[byte]string{}
8+
cnt := map[string]int{}
9+
for i, k := range keys {
10+
mp[k] = values[i]
11+
}
12+
e := Encrypter{mp, cnt}
13+
for _, v := range dictionary {
14+
e.cnt[e.Encrypt(v)]++
15+
}
16+
return e
17+
}
18+
19+
func (this *Encrypter) Encrypt(word1 string) string {
20+
var ans strings.Builder
21+
for _, c := range word1 {
22+
if v, ok := this.mp[byte(c)]; ok {
23+
ans.WriteString(v)
24+
} else {
25+
return ""
26+
}
27+
}
28+
return ans.String()
29+
}
30+
31+
func (this *Encrypter) Decrypt(word2 string) int {
32+
return this.cnt[word2]
33+
}
34+
35+
/**
36+
* Your Encrypter object will be instantiated and called as such:
37+
* obj := Constructor(keys, values, dictionary);
38+
* param_1 := obj.Encrypt(word1);
39+
* param_2 := obj.Decrypt(word2);
40+
*/

0 commit comments

Comments
 (0)