2026-04-25:反轉元音數相同的單詞。用go語言,給定一個由小寫英文單詞組成的字符串,各單詞之間用單空格分隔。
先統計第一個單詞里出現的元音字母數量(元音為 a/e/i/o/u)。記這個數量為 k。
然后從第二個單詞開始逐個處理:如果某個單詞的元音數量也等于 k,就把該單詞反轉字母順序;否則保持該單詞不變。
最后把所有單詞按原有順序重新用空格拼接,輸出結果字符串。
1 <= s.length <= 100000。
s 僅由小寫的英文字母和空格組成。
s 中的單詞由 單個空格 隔開。
s 不包含前導或尾隨空格。
輸入: s = "cat and mice"。
輸出: "cat dna mice"。
解釋:
第一個單詞 "cat" 包含 1 個元音字母。
"and" 包含 1 個元音字母,因此將其反轉為 "dna"。
"mice" 包含 2 個元音字母,因此保持不變。
最終結果字符串為 "cat dna mice"。
題目來自力扣3775。
代碼執行過程詳細分步描述 第一步:定義元音統計函數(countVowel)
這是一個輔助函數,作用是統計一個字符串中元音字母(a/e/i/o/u)的總個數:
1. 遍歷字符串里的每一個字符;
2. 逐個判斷字符是否是元音字母;
3. 每找到一個元音字母,計數加1;
4. 遍歷完成后,返回最終的元音計數結果。
代碼將輸入的完整字符串按單個空格切割,把連續的字符串拆分成獨立的單詞列表:
? 輸入:
"cat and mice"? 切割后得到單詞數組:
["cat", "and", "mice"]
1. 取出單詞數組的第一個單詞:
cat;2. 調用元音統計函數計算元音數:
? 字符
c:非元音;? 字符
a:元音,計數+1;? 字符
t:非元音;
3. 最終第一個單詞元音數k = 1,并把這個值固定下來。
子步驟3:遍歷處理第二個及之后的所有單詞
從第二個單詞開始(數組下標為1),逐個判斷、處理:
1.處理第二個單詞:and
? 調用函數統計元音數:
a是元音,n/d非元音,元音數=1;? 判斷:元音數(1)等于k(1),滿足反轉條件;
? 執行反轉操作:將單詞
and的字母順序顛倒,變成dna;? 用反轉后的單詞替換原單詞,數組變為:
["cat", "dna", "mice"]。
2.處理第三個單詞:mice
? 調用函數統計元音數:
i和e是元音,元音數=2;? 判斷:元音數(2)不等于k(1),不滿足反轉條件;
? 保持原單詞不變,數組仍為:
["cat", "dna", "mice"]。
將處理后的單詞數組,按單個空格重新拼接成一個完整字符串:
? 拼接結果:
cat dna mice。
1. 定義輸入字符串
s = "cat and mice";2. 調用核心處理函數得到結果;
3. 打印輸出最終結果:
cat dna mice。
時間復雜度描述代碼執行的總操作次數與輸入數據規模的關系:
1. 字符串拆分、拼接:操作次數與字符串總長度
n成正比,復雜度為O(n);2. 元音統計:遍歷整個字符串的所有字符一次,復雜度為O(n);
3. 單詞反轉:僅反轉符合條件的單詞,總操作次數不超過字符串總長度
n,復雜度為O(n);
所有步驟都是線性操作,總時間復雜度為 O(n)(n 為輸入字符串的總長度)。
2. 總額外空間復雜度
額外空間復雜度描述代碼執行過程中,除了輸入和輸出外,額外占用的內存空間:
1. 代碼需要創建單詞數組存儲所有單詞,空間大小與字符串長度
n成正比;2. 反轉單詞時需要創建臨時字節切片,空間大小不超過單詞總長度;
3. 所有臨時變量的空間占用均為線性級別;
總額外空間復雜度為 O(n)(n 為輸入字符串的總長度)。
總結
1. 執行流程:拆分字符串→統計首單詞元音數→遍歷判斷并反轉符合條件的單詞→拼接字符串→輸出結果;
2. 時間復雜度:O(n),處理效率高,可滿足題目中最長10萬字符的輸入要求;
3. 額外空間復雜度:O(n),需要線性空間存儲單詞和臨時數據。
package main
import (
"fmt"
"slices"
"strings"
)
func countVowel(s string) (vowel int) {
for _, c := range s {
if strings.IndexRune("aeiou", c) >= 0 {
vowel++
}
}
return
}
func reverseWords(s string)string {
a := strings.Split(s, " ")
cnt0 := countVowel(a[0])
for i := 1; i < len(a); i++ {
if countVowel(a[i]) == cnt0 {
t := []byte(a[i])
slices.Reverse(t)
a[i] = string(t)
}
}
return strings.Join(a, " ")
}func main() {
s := "cat and mice"
result := reverseWords(s)
fmt.Println(result)
}
Python完整代碼如下:
# -*-coding:utf-8-*-
def count_vowel(s: str) -> int:
"""Count vowels in a string"""
vowel = 0
for c in s:
if c in "aeiou":
vowel += 1
return vowel
def reverse_words(s: str) -> str:
"""Reverse words that have the same vowel count as the first word"""
words = s.split(" ")
cnt0 = count_vowel(words[0])
for i in range(1, len(words)):
if count_vowel(words[i]) == cnt0:
# Reverse the word
words[i] = words[i][::-1]
return" ".join(words)
def main():
s = "cat and mice"
result = reverse_words(s)
print(result)if __name__ == "__main__":
main()
C++完整代碼如下:
int countVowel(const std::string& s) {
int vowel = 0;
std::string vowels = "aeiou";
for (char c : s) {
if (vowels.find(c) != std::string::npos) {
vowel++;
}
}
return vowel;
}std::string reverseWords(const std::string& s) {
// Split string into words
std::vector string > words;
std::stringstream ss(s);
std:: string word;
while (ss >> word) {
words.push_back(word);
}
if (words.empty()) {
return "" ;
}
int cnt0 = countVowel(words[ 0 ]);
for (size_t i = 1 ; i < words.size(); i++) {
if (countVowel(words[i]) == cnt0) {
// Reverse the word
std::reverse(words[i].begin(), words[i].end());
}
}
// Join the words back
std:: string result;
for (size_t i = 0 ; i < words.size(); i++) {
if (i > 0 ) {
result += " " ;
}
result += words[i];
}
return result;
}
int main() {
std:: string s = "cat and mice" ;
std:: string result = reverseWords(s);
std::cout << result << std::endl;
return 0 ;
}
我們相信人工智能為普通人提供了一種“增強工具”,并致力于分享全方位的AI知識。在這里,您可以找到最新的AI科普文章、工具評測、提升效率的秘籍以及行業洞察。 歡迎關注“福大大架構師每日一題”,發消息可獲得面試資料,讓AI助力您的未來發展。
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.