C#LeetCode刷题之#824-山羊拉丁文(Goat Latin)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3971 访问。
给定一个由空格分割单词的句子 S。每个单词只包含大写或小写字母。
我们要将句子转换为 “Goat Latin”(一种类似于 猪拉丁文 - Pig Latin 的虚构语言)。
山羊拉丁文的规则如下:
如果单词以元音开头(a, e, i, o, u),在单词后添加"ma"。
例如,单词"apple"变为"applema"。
如果单词以辅音字母开头(即非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
例如,单词"goat"变为"oatgma"。
根据单词在句子中的索引,在单词最后添加与索引相同数量的字母'a',索引从1开始。
例如,在第一个单词后添加"a",在第二个单词后添加"aa",以此类推。
返回将 S 转换为山羊拉丁文后的句子。
输入: "I speak Goat Latin"
输出: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
输入: "The quick brown fox jumped over the lazy dog"
输出: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
说明:
S 中仅包含大小写字母和空格。单词间有且仅有一个空格。
1 <= S.length <= 150。
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only.
We would like to convert the sentence to "Goat Latin" (a made-up language similar to Pig Latin.)
The rules of Goat Latin are as follows:
If a word begins with a vowel (a, e, i, o, or u), append "ma" to the end of the word.
For example, the word 'apple' becomes 'applema'.
If a word begins with a consonant (i.e. not a vowel), remove the first letter and append it to the end, then add "ma".
For example, the word "goat" becomes "oatgma".
Add one letter 'a' to the end of each word per its word index in the sentence, starting with 1.
For example, the first word gets "a" added to the end, the second word gets "aa" added to the end and so on.
Return the final sentence representing the conversion from S to Goat Latin.
Input: "I speak Goat Latin"
Output: "Imaa peaksmaaa oatGmaaaa atinLmaaaaa"
Input: "The quick brown fox jumped over the lazy dog"
Output: "heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa"
Notes:
- S contains only uppercase, lowercase and spaces. Exactly one space between each word.
- 1 <= S.length <= 150.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3971 访问。
public class Program {
public static void Main(string[] args) {
var S = "I speak Goat Latin";
var res = ToGoatLatin(S);
Console.WriteLine(res);
Console.ReadKey();
}
private static string ToGoatLatin(string S) {
//按空格分隔
var split = S.Split(' ');
//定义结果
var res = string.Empty;
for(var i = 0; i < split.Length; i++) {
if(IsStartsWithVowel(split[i])) {
res += split[i];
} else {
//辅音字母开头时,首字母后置
res += split[i].Substring(1) + split[i][0];
}
//追回字符串 ma 和按索引重复的字符 a
res += "ma" + RepeatString(i + 1) + " ";
}
return res.Trim();
}
private static bool IsStartsWithVowel(string word) {
//判断是不是元音字母
var s = word[0].ToString().ToLower();
return s == "a" || s == "e" || s == "i" || s == "o" || s == "u";
}
private static string RepeatString(int count) {
//重复字符串
var c = 'a';
var res = string.Empty;
for(var i = 0; i < count; i++) {
res += c;
}
return res;
}
}
以上给出1种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3971 访问。
Imaa peaksmaaa oatGmaaaa atinLmaaaaa
分析:
显而易见,以上算法的时间复杂度为: 。
C#LeetCode刷题之#824-山羊拉丁文(Goat Latin)的更多相关文章
- [Swift]LeetCode824. 山羊拉丁文 | Goat Latin
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and up ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- Java实现 LeetCode 824 山羊拉丁文(暴力)
824. 山羊拉丁文 给定一个由空格分割单词的句子 S.每个单词只包含大写或小写字母. 我们要将句子转换为 "Goat Latin"(一种类似于 猪拉丁文 - Pig Latin ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
随机推荐
- mybatis的<if>标签,<foreach>标签,<collection>标签,<association>标签以及useGeneratedKeys用法
<if>标签 1.判断非空或不等于 <if test="assessTypes!= null and assessTypes!='' "> AND FIND ...
- oracle 启动报ORA-01105 ORA-19808
bash-4.4$ srvctl start instance -i jfcddb2 -d jfcddb PRCR-1013 : Failed to start resource ora.jfcddb ...
- python怎么自学?今日头条技术大佬的真实经历分享
大家好,我是武州,27岁,目前在字节跳动担任Python后端工程师一职. (摆拍一下,假装是保安) 在开始今天的文章之前,不知道你们有没有遇到过这样的问题: 大学没学到什么实质技术,毕业后找不到高薪的 ...
- idea 导入eclipse play1.2.7项目
1.play eclipsify #myapp 转为eclipse目录结构 2.导入eclipse,一路next. 3.新增个Application -Xms1536m-Xmx2048m-Xdebug ...
- Go的安装和使用
1.安装环境 进入Golang官网,进入下载页面. (如果打不开可访问Golang中国,或者Golang中文版,或者百度网盘,提取码:wfw5) 根据实际需求选择版本进行下载. 运行下载好的MSI安装 ...
- 安装nginx1.10和状态模块
环境 操作系统: Centos7.2 内核: 3.10.0-327.el7.x86_64 nginx: nginx-1.10.0.tar.gz 安装: 1.安装依赖包 yum -y install g ...
- PHP设计模式之----简单工厂模式
定义个抽象的类(或接口),让子类去继承(实现)它 abstract class Operation { abstract public function getValue($num1, $num2); ...
- Android:自定义BaseActivity基类
使用BaseActivity可以封装一些重复代码例如设置标题栏颜色,封装一些工具类... 主要功能: 封装Toast 新建一个BaseActivity继承自Activity package com.o ...
- PHP jdtojewish() 函数
------------恢复内容开始------------ 实例 把儒略日计数转换为犹太历法的日期: <?php$jd=jdtojewish(1789430); echo $jd;?> ...
- ORACLE表与表联接的几种方式
三大表与表联接方式 1.NESTED LOOPS 嵌套循环 2.HASH JOIN 哈希联接 3.SORT MERGE 排序合并联接 1.NESTED LOOPS 嵌套循环 嵌套循环的本质是将外部数 ...