LeetCode 题解工作台

转换成小写字母

给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。 示例 1: 输入: s = "Hello" 输出: "hello" 示例 2: 输入: s = "here" 输出: "here" 示例 3: 输入: s = "LOVELY" 输出: "lovely" 提示: 1 …

category

1

题型

code_blocks

7

代码语言

hub

3

相关题

当前训练重点

简单 · String-driven solution strategy

bolt

答案摘要

我们可以遍历字符串,对于每个大写字母,将其转换为小写字母。最后返回转换后的字符串即可。 时间复杂度 ,其中 为字符串的长度。忽略答案的空间消耗,空间复杂度 。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 String-driven solution strategy 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

 

示例 1:

输入:s = "Hello"
输出:"hello"

示例 2:

输入:s = "here"
输出:"here"

示例 3:

输入:s = "LOVELY"
输出:"lovely"

 

提示:

  • 1 <= s.length <= 100
  • s 由 ASCII 字符集中的可打印字符组成
lightbulb

解题思路

方法一:模拟

我们可以遍历字符串,对于每个大写字母,将其转换为小写字母。最后返回转换后的字符串即可。

时间复杂度 O(n)O(n),其中 nn 为字符串的长度。忽略答案的空间消耗,空间复杂度 O(1)O(1)

1
2
3
4
class Solution:
    def toLowerCase(self, s: str) -> str:
        return "".join([chr(ord(c) | 32) if c.isupper() else c for c in s])
speed

复杂度分析

指标
时间Depends on the final approach
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Candidates should demonstrate a solid understanding of string manipulation and be able to articulate trade-offs in terms of efficiency and readability.

  • question_mark

    The problem tests a candidate's ability to think about edge cases, like handling strings that contain no uppercase characters.

  • question_mark

    The interviewer will look for clarity in how the candidate justifies their choice of approach, particularly regarding the use of built-in methods vs. manual implementations.

warning

常见陷阱

外企场景
  • error

    Candidates may use the built-in function without understanding its internal workings, missing an opportunity to demonstrate algorithmic thinking.

  • error

    Forgetting to handle cases where no uppercase letters exist in the string can lead to unnecessary processing or incorrect assumptions.

  • error

    Failing to optimize for space complexity by repeatedly modifying the string (in languages that don't handle immutability) can lead to inefficient solutions.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Convert a string to uppercase instead of lowercase.

  • arrow_right_alt

    Handle case insensitivity by converting both uppercase and lowercase characters to a common case.

  • arrow_right_alt

    Process a string in place to reduce space complexity, if mutable strings are allowed.

help

常见问题

外企场景

转换成小写字母题解:String-driven solution … | LeetCode #709 简单