Skip to content

Commit 8aa28e0

Browse files
authored
feat: add php solution to lc problem: No.0451 (#1002)
1 parent bfc6b34 commit 8aa28e0

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

solution/0400-0499/0451.Sort Characters By Frequency/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,28 @@ impl Solution {
178178
}
179179
```
180180

181+
### **PHP**
182+
183+
```php
184+
class Solution {
185+
/**
186+
* @param String $s
187+
* @return String
188+
*/
189+
function frequencySort($s) {
190+
for ($i = 0; $i < strlen($s); $i++) {
191+
$hashtable[$s[$i]] += 1;
192+
}
193+
arsort($hashtable);
194+
$keys = array_keys($hashtable);
195+
for ($j = 0; $j < count($keys); $j++) {
196+
$rs = $rs.str_repeat($keys[$j], $hashtable[$keys[$j]]);
197+
}
198+
return $rs;
199+
}
200+
};
201+
```
202+
181203
### **...**
182204

183205
```

solution/0400-0499/0451.Sort Characters By Frequency/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,28 @@ impl Solution {
162162
}
163163
```
164164

165+
### **PHP**
166+
167+
```php
168+
class Solution {
169+
/**
170+
* @param String $s
171+
* @return String
172+
*/
173+
function frequencySort($s) {
174+
for ($i = 0; $i < strlen($s); $i++) {
175+
$hashtable[$s[$i]] += 1;
176+
}
177+
arsort($hashtable);
178+
$keys = array_keys($hashtable);
179+
for ($j = 0; $j < count($keys); $j++) {
180+
$rs = $rs.str_repeat($keys[$j], $hashtable[$keys[$j]]);
181+
}
182+
return $rs;
183+
}
184+
};
185+
```
186+
165187
### **...**
166188

167189
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param String $s
4+
* @return String
5+
*/
6+
function frequencySort($s) {
7+
for ($i = 0; $i < strlen($s); $i++) {
8+
$hashtable[$s[$i]] += 1;
9+
}
10+
arsort($hashtable);
11+
$keys = array_keys($hashtable);
12+
for ($j = 0; $j < count($keys); $j++) {
13+
$rs = $rs.str_repeat($keys[$j], $hashtable[$keys[$j]]);
14+
}
15+
return $rs;
16+
}
17+
};

0 commit comments

Comments
 (0)