Skip to content

Commit 72f5f58

Browse files
authored
feat: add php solution to lc problem: No.1451 (#1020)
1 parent 76dc15a commit 72f5f58

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

solution/1400-1499/1451.Rearrange Words in a Sentence/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ func arrangeWords(text string) string {
137137
}
138138
```
139139

140+
### **PHP**
141+
142+
```php
143+
class Solution {
144+
/**
145+
* @param String $text
146+
* @return String
147+
*/
148+
function arrangeWords($text) {
149+
$text = lcfirst($text);
150+
$arr = explode(" ", $text);
151+
for ($i = 0; $i < count($arr); $i++) {
152+
$hashtable[$i] = strlen($arr[$i]);
153+
}
154+
asort($hashtable);
155+
$key = array_keys($hashtable);
156+
$rs = [];
157+
for ($j = 0; $j < count($key); $j++) {
158+
array_push($rs, $arr[$key[$j]]);
159+
}
160+
return ucfirst(implode(" ", $rs));
161+
}
162+
}
163+
```
164+
140165
### **...**
141166

142167
```

solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md

+25
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ func arrangeWords(text string) string {
122122
}
123123
```
124124

125+
### **PHP**
126+
127+
```php
128+
class Solution {
129+
/**
130+
* @param String $text
131+
* @return String
132+
*/
133+
function arrangeWords($text) {
134+
$text = lcfirst($text);
135+
$arr = explode(" ", $text);
136+
for ($i = 0; $i < count($arr); $i++) {
137+
$hashtable[$i] = strlen($arr[$i]);
138+
}
139+
asort($hashtable);
140+
$key = array_keys($hashtable);
141+
$rs = [];
142+
for ($j = 0; $j < count($key); $j++) {
143+
array_push($rs, $arr[$key[$j]]);
144+
}
145+
return ucfirst(implode(" ", $rs));
146+
}
147+
}
148+
```
149+
125150
### **...**
126151

127152
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
/**
3+
* @param String $text
4+
* @return String
5+
*/
6+
function arrangeWords($text) {
7+
$text = lcfirst($text);
8+
$arr = explode(" ", $text);
9+
for ($i = 0; $i < count($arr); $i++) {
10+
$hashtable[$i] = strlen($arr[$i]);
11+
}
12+
asort($hashtable);
13+
$key = array_keys($hashtable);
14+
$rs = [];
15+
for ($j = 0; $j < count($key); $j++) {
16+
array_push($rs, $arr[$key[$j]]);
17+
}
18+
return ucfirst(implode(" ", $rs));
19+
}
20+
}

0 commit comments

Comments
 (0)