diff --git a/solution/1400-1499/1451.Rearrange Words in a Sentence/README.md b/solution/1400-1499/1451.Rearrange Words in a Sentence/README.md index c69dae7c508aa..222c2373d7b97 100644 --- a/solution/1400-1499/1451.Rearrange Words in a Sentence/README.md +++ b/solution/1400-1499/1451.Rearrange Words in a Sentence/README.md @@ -137,6 +137,31 @@ func arrangeWords(text string) string { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $text + * @return String + */ + function arrangeWords($text) { + $text = lcfirst($text); + $arr = explode(" ", $text); + for ($i = 0; $i < count($arr); $i++) { + $hashtable[$i] = strlen($arr[$i]); + } + asort($hashtable); + $key = array_keys($hashtable); + $rs = []; + for ($j = 0; $j < count($key); $j++) { + array_push($rs, $arr[$key[$j]]); + } + return ucfirst(implode(" ", $rs)); + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md b/solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md index 241ee7b53db80..748ec4169cd07 100644 --- a/solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md +++ b/solution/1400-1499/1451.Rearrange Words in a Sentence/README_EN.md @@ -122,6 +122,31 @@ func arrangeWords(text string) string { } ``` +### **PHP** + +```php +class Solution { + /** + * @param String $text + * @return String + */ + function arrangeWords($text) { + $text = lcfirst($text); + $arr = explode(" ", $text); + for ($i = 0; $i < count($arr); $i++) { + $hashtable[$i] = strlen($arr[$i]); + } + asort($hashtable); + $key = array_keys($hashtable); + $rs = []; + for ($j = 0; $j < count($key); $j++) { + array_push($rs, $arr[$key[$j]]); + } + return ucfirst(implode(" ", $rs)); + } +} +``` + ### **...** ``` diff --git a/solution/1400-1499/1451.Rearrange Words in a Sentence/Solution.php b/solution/1400-1499/1451.Rearrange Words in a Sentence/Solution.php new file mode 100644 index 0000000000000..9017a6fed9583 --- /dev/null +++ b/solution/1400-1499/1451.Rearrange Words in a Sentence/Solution.php @@ -0,0 +1,20 @@ +class Solution { + /** + * @param String $text + * @return String + */ + function arrangeWords($text) { + $text = lcfirst($text); + $arr = explode(" ", $text); + for ($i = 0; $i < count($arr); $i++) { + $hashtable[$i] = strlen($arr[$i]); + } + asort($hashtable); + $key = array_keys($hashtable); + $rs = []; + for ($j = 0; $j < count($key); $j++) { + array_push($rs, $arr[$key[$j]]); + } + return ucfirst(implode(" ", $rs)); + } +} \ No newline at end of file