You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

string类型数组自动生成:生成长度为k的同字符字母数组需求

Got it, let's tackle this problem! You want to generate an array of strings where each string is made up of the same repeated letter, with the length of each string being a runtime input k. And the array should cover every letter from 'a' to 'z'—so for k=2 it's ["aa", "bb", ..., "zz"], k=3 it's ["aaa", "bbb", ..., "zzz"], right?

I'll share implementations in two common languages to cover different use cases:

Solution in Python

This is super straightforward thanks to Python's string repetition and built-in helper for letters.

Here's a clean function that does exactly what you need:

import string

def generate_same_letter_strings(k):
    # Validate input first (optional but recommended)
    if not isinstance(k, int) or k <= 0:
        raise ValueError("k must be a positive integer")
    # Build the array by repeating each lowercase letter k times
    return [letter * k for letter in string.ascii_lowercase]

# Example runtime usage
k = int(input("Enter the length k: "))
result = generate_same_letter_strings(k)
print(result)

How it works:

  • string.ascii_lowercase gives us the full string of lowercase letters: "abcdefghijklmnopqrstuvwxyz"
  • Python's string multiplication (letter * k) repeats the character exactly k times to form each element
  • The list comprehension builds the entire array in one concise line

If you need uppercase letters instead, just swap string.ascii_lowercase with string.ascii_uppercase.


Solution in JavaScript

For JavaScript environments, here's an equivalent implementation:

function generateSameLetterStrings(k) {
    // Input validation
    if (typeof k !== 'number' || k <= 0 || !Number.isInteger(k)) {
        throw new Error("k must be a positive integer");
    }
    const result = [];
    // Loop through ASCII codes for 'a' (97) to 'z' (122)
    for (let charCode = 97; charCode <= 122; charCode++) {
        const letter = String.fromCharCode(charCode);
        // Repeat the letter k times and add to the array
        result.push(letter.repeat(k));
    }
    return result;
}

// Example runtime usage
const k = parseInt(prompt("Enter the length k:"));
console.log(generateSameLetterStrings(k));

How it works:

  • We loop through the ASCII values corresponding to lowercase letters
  • String.fromCharCode() converts each ASCII code to its actual character
  • The repeat() method creates a string of length k from the single letter, which we push to our result array

Quick Edge Case Notes

  • If k is 0: Both implementations will return an array of empty strings unless you add validation (which the examples above do)
  • For negative k: Most languages handle this by returning empty strings, so the input checks in the code prevent that from happening

内容的提问来源于stack exchange,提问作者Avinash

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:35:13