相对开音节构成的结构为辅音+元音(aeiou)+辅音(r除外)
常见的单词有bike cake
给定一个字符串,以空格为分隔符
反转每个单词的字母
若单词中包含如数字等其他非字母时不进行反转
反转后计算其中含有相对开音节结构的子串个数
(连续子串中部分字符可以重复)
字符串 以空格分割的多个单词
长度<10000 字母只考虑小写
含有相对开音节结构的子串个数
ekam a ekac
2
反转后为make a cake其中make和cake为相对开音节子串
返回2
!ekam a ekekac
2
反转后为 !ekam a cakeke
因为!ekam含有非英文字母,所以未反转
其中 cake和keke 为相对开音节子串 返回2
import java.util.Scanner;
/**
* Created with IntelliJ IDEA.
* Author: Amos
* E-mail: amos@amoscloud.com
* Date: 2021/4/23
* Time: 16:12
* Description:
*/
public class Main0045 {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
String line = scanner.nextLine();
solution(line);
}
}
private static void solution(String line) {
String[] words = line.split(" ");
int count = 0;
for (String word : words) {
char[] chars = word.toCharArray();
if (word.replaceAll("[a-z]+", "").isEmpty()) {
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
char tmp = chars[i];
chars[i] = chars[j];
chars[j] = tmp;
}
}
if (chars.length < 4) continue;
for (int i = 0; i < chars.length - 3; i++) {
if (isWord(chars[i])
&& isWord(chars[i + 1])
&& isWord(chars[i + 2])
&& isWord(chars[i + 3])
)
if (!isVowel(chars[i])
&& isVowel(chars[i + 1])
&& !isVowel(chars[i + 2]) && chars[i + 2] != 'r'
&& chars[i + 3] == 'e') {
count++;
}
}
}
System.out.print(count);
}
private static boolean isVowel(char c) {
return c == 'a' || c == 'e' || c == 'i'
|| c == 'o' || c == 'u';
}
private static boolean isWord(char c) {
return c <= 'z' && c >= 'a';
}
}