当前位置:网站首页>LeetCode 1374. Generate an odd number of each character string
LeetCode 1374. Generate an odd number of each character string
2022-08-01 18:20:00 【Tisfy】
【LetMeFly】1374.生成每种字符都是奇数个的字符串
力扣题目链接:https://leetcode.cn/problems/generate-a-string-with-characters-that-have-odd-counts/
给你一个整数 n
,请你返回一个含n
个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次.
返回的字符串必须只含小写英文字母.如果存在多个满足题目要求的字符串,则返回其中任意一个即可.
示例 1:
输入:n = 4 输出:"pppz" 解释:"pppz" 是一个满足题目要求的字符串,因为 'p' 出现 3 次,且 'z' 出现 1 次.当然,还有很多其他字符串也满足题目要求,比如:"ohhh" 和 "love".
示例 2:
输入:n = 2 输出:"xy" 解释:"xy" 是一个满足题目要求的字符串,因为 'x' 和 'y' 各出现 1 次.当然,还有很多其他字符串也满足题目要求,比如:"ag" 和 "ur".
示例 3:
输入:n = 7 输出:"holasss"
提示:
1 <= n <= 500
方法一:构造
- If the length of the string is odd,Then every character in the string is
a
meet the requirements of the subject; - If the length of the string is even( n n n),那么 n − 1 n-1 n−1为奇数,返回 n − 1 n-1 n−1个
a
和 1 1 1个b
meet the requirements of the subject.
(PS: syntactic sugar available——三木运算符 一行代码解决)
- 时间复杂度 O ( n ) O(n) O(n)
- 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
C++
class Solution {
public:
string generateTheString(int n) {
return n % 2 ? string(n, 'a') : string(n - 1, 'a') + 'b';
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126093645
边栏推荐
猜你喜欢
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
【报错】Uncaught (in promise) TypeError: Cannot read properties of undefined (reading ‘concat‘)
QT_QThread thread
深入浅出Flask PIN
How to build a CMDB driven by consumption scenarios?
Leetcode74. 搜索二维矩阵
Zabbix6.0 DingTalk robot alarm
MySQL Lock wait timeout exceeded; try restarting transaction 锁等待
B001 - Intelligent ecological fish tank based on STM32
B011 - 51-based multifunctional fingerprint smart lock
随机推荐
打开微信客服
【Day_09 0427】走方格的方案数
How many steps does it take to convert an ENS domain name into music?
QT_QDialog 对话框
消息模板占位符的使用
QT_QThread线程
Summer vacation first week wrap-up blog
What is the JVM runtime data area and the JMM memory model
QPalette调色板、框架色彩填充
Detailed explanation of DBPack SQL Tracing function and data encryption function
Leetcode73. Matrix Zeroing
C#/VB.NET: extracted from the PDF document all form
OnePlus 10RT appears on Geekbench, product launch also seems to be approaching
国标GB28181协议EasyGBS平台兼容老版本收流端口的功能实现
塔防海岸线用户协议
【Day_11 0506】 最近公共祖先
Leetcode73. 矩阵置零
Basic image processing in opencv
EpiSci|片上系统的深度强化学习:神话与现实
Leetcode75. 颜色分类