From 79a646407fbeb1f4883f2b7c1fed6ff47c4144e5 Mon Sep 17 00:00:00 2001 From: Qiu-IT Date: Sun, 4 Jun 2023 17:42:45 +0200 Subject: [PATCH] feat: add php solution to lc problem: No.1451 --- .../README.md | 25 +++++++++++++++++++ .../README_EN.md | 25 +++++++++++++++++++ .../Solution.php | 20 +++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 solution/1400-1499/1451.Rearrange Words in a Sentence/Solution.php 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