当前位置:网站首页>【 LeetCode 】 1374. Generate each character string is an odd number
【 LeetCode 】 1374. Generate each character string is an odd number
2022-08-02 22:35:00 【通过晚上】
题目
给你一个整数 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
思路
- 分类讨论,若 n n n为奇数,则直接返回 n n n个
a - 若 n n n为偶数则返回 n − 1 n-1 n−1个
a和 1 1 1个b
代码
class Solution:
def generateTheString(self, n: int) -> str:
if n % 2 == 1: return "a"*n
else: return "a"*(n-1) + "b"
复杂度
- 时间复杂度: O ( n ) O(n) O(n)
- 空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
猜你喜欢

A Review of Nature Microbiology: Focusing on the Algae--Ecological Interface of Phytoplankton-Bacteria Interactions

「面试必会」这应该是最有深度的TCP三次握手、四次挥手细节讲解

AI科学家:自动发现物理系统的隐藏状态变量

实现客户服务自助,打造产品知识库

NC | 土壤微生物组的结构和功能揭示全球湿地N2O释放

平稳发展 | 西欧地区手游玩家的数据和洞察

Geoserver + mysql + openlayers problem

JVM内存和垃圾回收-05.虚拟机栈

动态折线图,制作原来是这么简单

连续三次 | 灵雀云入选Gartner中国ICT技术成熟度曲线报告
随机推荐
golang刷leetcode 经典(13) 最小高度树
spack install reports an error /tmp/ccBDQNaB.s: Assembler message:
ShardingSphere-proxy +PostgreSQL实现读写分离(静态策略)
ALV报表学习总结
分享一个 web 应用版本监测 (更新) 的工具库
SCANIA SCANIA OTL tag is introduced
Geoserver+mysql+openlayers
J9 digital theory: the Internet across chain bridge has what effect?
Redis集群配置
Brain-computer interface 003 | Musk said that he has realized a virtual self-dialogue with the cloud, and related concept shares have risen sharply
Parse the commonly used methods in the List interface that are overridden by subclasses
A Review of Nature Microbiology: Focusing on the Algae--Ecological Interface of Phytoplankton-Bacteria Interactions
遇上Mysql亿级优化,怎么办
松鼠短视频系统为用户加入随机头像代码-快速为用户加上随机头衔
JVM内存和垃圾回收-05.虚拟机栈
如何ES源码中添加一个自己的API 流程梳理
Nature Microbiology综述:聚焦藻际--浮游植物和细菌互作的生态界面
7.25 - 每日一题 - 408
VMware虚拟机无法上网
Based on OpenGL glaciers and firebird (illumination calculation model, visual, particle system)