最长元音子串长度

最长元音子串长度

 1process.stdin.resume();  2process.stdin.setEncoding('utf-8');  3let input = '';  4  5process.stdin.on('data', (data) => {  6 input += data;  7});  8  9process.stdin.on('end', () => { 10 // 去除输入字符串两端的空白字符(如果有的话) 11 const trimmedInput = input.trim(); 12 13 // 实现查找最长元音子串长度的函数 14 function findLongestVowelSubstringLength(str) { 15 const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); 16 let maxLength = 0; 17 let currentLength = 0; 18 19 for (let char of str) { 20 if (vowels.has(char)) { 21 // 如果是元音字母,增加当前元音子串的长度 22 currentLength++; 23 // 更新最长元音子串的长度 24 maxLength = Math.max(maxLength, currentLength); 25 } else { 26 // 如果不是元音字母,重置当前元音子串的长度 27 currentLength = 0; 28 } 29 } 30 31 return maxLength; 32 } 33 34 // 调用函数并输出结果 35 const result = findLongestVowelSubstringLength(trimmedInput); 36 console.log(result); 37 38 process.exit(); 39});
process.stdin.resume(); process.stdin.setEncoding('utf-8'); let input = ''; process.stdin.on('data', (data) => { input += data; }); process.stdin.on('end', () => { // 去除输入字符串两端的空白字符(如果有的话) const trimmedInput = input.trim(); // 实现查找最长元音子串长度的函数 function findLongestVowelSubstringLength(str) { const vowels = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']); let maxLength = 0; let currentLength = 0; for (let char of str) { if (vowels.has(char)) { // 如果是元音字母,增加当前元音子串的长度 currentLength++; // 更新最长元音子串的长度 maxLength = Math.max(maxLength, currentLength); } else { // 如果不是元音字母,重置当前元音子串的长度 currentLength = 0; } } return maxLength; } // 调用函数并输出结果 const result = findLongestVowelSubstringLength(trimmedInput); console.log(result); process.exit(); });

Powered By 可尔物语

浙ICP备11005866号-12