Leetcode PHP题解--D60 824. Goat Latin

栏目: PHP · 发布时间: 6年前

内容简介:给定一个句子,由大小写英文字母组成,以空格为单词的分割。按以下规则修改单词:

D60 824. Goat Latin

题目链接

824. Goat Latin

题目分析

给定一个句子,由大小写英文字母组成,以空格为单词的分割。

按以下规则修改单词:

ma
ma
a

思路

首先,需要把句子分割成单词。用str_explode就可以实现。

分割后,判断首字母是否不是元音。

不是元音,则将第一个字母移到最后。

给字符串末尾添加 ma

给字符串末尾添加额外的 na

最终代码

<?php
class Solution {

    /**
     * @param String $S
     * @return String
     */
    function toGoatLatin($S) {
        $words = explode(' ', $S);
        $newWords = [];
        foreach($words as $key => $word){
            if(!in_array($word[0],['a','e','i','o','u','A','E','I','O','U'])){
                $word .= $word[0];
                $word = substr($word,1);
            }
            $word .= 'ma'.str_repeat('a', $key+1);
            $newWords[] = $word;
        }
        return implode(' ', $newWords);
    }
}

若觉得本文章对你有用,欢迎用 爱发电 资助。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to Probability Theory and Its Applications

An Introduction to Probability Theory and Its Applications

William Feller / Wiley / 1991-1-1 / USD 120.00

Major changes in this edition include the substitution of probabilistic arguments for combinatorial artifices, and the addition of new sections on branching processes, Markov chains, and the De Moivre......一起来看看 《An Introduction to Probability Theory and Its Applications》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器