当前位置:网站首页>每日一题(2022-04-21)——山羊拉丁文
每日一题(2022-04-21)——山羊拉丁文
2022-04-23 02:06:00 【用脑袋装水】
目录
824. 山羊拉丁文
题目描述:
给你一个由若干单词组成的句子 sentence ,单词间由空格分隔。每个单词仅由大写和小写英文字母组成。
请你将句子转换为 “山羊拉丁文(Goat Latin,返回将 sentence 转换为山羊拉丁文后的句子。
山羊拉丁文的规则如下:
- 如果单词以元音开头(‘a’, ‘e’, ‘i’, ‘o’, ‘u’),在单词后添加"ma"。
- 例如,单词 “apple” 变为 “applema” 。
- 如果单词以辅音字母开头(即,非元音字母),移除第一个字符并将它放到末尾,之后再添加"ma"。
- 例如,单词 “goat” 变为 “oatgma” 。
- 根据单词在句子中的索引,在单词最后添加与索引相同数量的字母’a’,索引从 1 开始。
- 例如,在第一个单词后添加 “a” ,在第二个单词后添加 “aa” ,以此类推。
示例1:
输入:sentence = “I speak Goat Latin”
输出:“Imaa peaksmaaa oatGmaaaa atinLmaaaaa”
示例2:
输入:sentence = “The quick brown fox jumped over the lazy dog”
输出:“heTmaa uickqmaaa rownbmaaaa oxfmaaaaa umpedjmaaaaaa overmaaaaaaa hetmaaaaaaaa azylmaaaaaaaaa ogdmaaaaaaaaaa”
题解:
func toGoatLatin(sentence string) string {
assistStr := "aeiouAEIOU"
words := strings.Split(sentence, " ")
ansSlice:=make([]string,len(words))
// 遍历words,对应的i,同样也是变化后的word在ansSlice切片中的位置
for i, word := range words {
index := i + 1
if strings.Contains(assistStr, string(word[0])) {
word = addA(word+"ma", index)
ansSlice[i] = word
continue
} else {
word = addA(word[1:]+string(word[0])+"ma", index)
ansSlice[i] = word
continue
}
}
return strings.Join(ansSlice," ")
}
// word后追加a,index是多少,追加几个“a”
func addA(word string, index int) string {
for i := 0; i < index; i++ {
word = word + "a"
}
return word
}
提交结果:
版权声明
本文为[用脑袋装水]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_52000204/article/details/124329461
边栏推荐
- What are the test steps of dynamic proxy IP?
- 006_redis_jedis快速入门
- Cc2541 emulator CC debugger tutorial
- 89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
- Shardingsphere broadcast table and binding table
- 代理IP可用率是不是等同于代理IP的效率?
- What business scenarios will the BGP server be used in?
- Batch multiple files into one hex
- PID refinement
- Esp32 message queue using FreeRTOS
猜你喜欢
NPM yarn startup error [resolved]
Dynamic memory management
What problems will you encounter when dialing VPS?
What categories do you need to know before using proxy IP?
012_ Access denied for user ‘root‘@‘localhost‘ (using password: YES)
Nanny level tutorial on building personal home page (II)
010_StringRedisTemplate
What is a dial-up server and what is its use?
013_基于Session实现短信验证码登录流程分析
What businesses use physical servers?
随机推荐
PHP & laravel & master several ways of generating token by API and some precautions (PIT)
教程】如何用GCC“零汇编”白嫖MDK
[Leetcode每日一题]396. 旋转函数
Shardingsphere sub database and sub table
[experience tutorial] Alipay balance automatically transferred to the balance of treasure how to set off, cancel Alipay balance automatically transferred to balance treasure?
【Chrome扩展程序】content_script的跨域问题
Is the availability of proxy IP equal to the efficiency of proxy IP?
Use of push() and pop()
2022.4.10-----leetcode. eight hundred and four
Leetcode40 - total number of combinations II
EBS:PO_EMPLOYEE_HIERARCHIES_ALL
不断下沉的咖啡业,是虚假的繁荣还是破局的前夜?
How to initialize "naming and surname" in C language
拨号服务器是什么,有什么用处?
使用代理IP是需要注意什么?
php 2022年4月20面试题整理
动态代理ip的测试步骤有哪些?
2022.4.20-----leetcode. three hundred and eighty-eight
中金财富是国企吗,开户安全吗
BGP服务器在什么业务场景会被用到?