To Lower Case
💻 coding

To Lower Case

1 min read 103 words
1 min read
ShareWhatsAppPost on X
  • 1The ToLowerCase() function converts a given string to lowercase.
  • 2It uses a character array to modify uppercase letters based on their ASCII values.
  • 3The function has a time complexity of O(n) and a space complexity of O(n).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The ToLowerCase() function converts a given string to lowercase."

To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:

Input: "Hello"

Output: "hello"

Example 2:

Input: "here"

Output: "here"

Example 3:

Input: "LOVELY"

Output: "lovely"

Solution:

using System;
using System.Collections.Generic;
using System.Text;

namespace LeetCode.AskGif.Easy.String
{
 public class ToLowerCaseSoln
 {
 public string ToLowerCase(string str)
 {
 var charArr = str.ToCharArray();
 int diff = 'a' - 'A';
 for (int i = 0; i < str.Length; i++)
 {
 char ch = charArr[i];
 if (ch >= 65 && ch <= 90)
 charArr[i] = (char)(ch + diff);
 }

 return new string(charArr);
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 8 May 2020 · 1 min read · 103 words

Part of AskGif Blog · coding

You might also like