前言
《华为OD笔试真题 JAVA》 专栏含华为OD机试真题JAVA实现、华为面试题、牛客网华为专栏真题。
如果您正在准备华为的面试,或者华为od的机会,希望可以帮到您! PS:文中答案仅供参考,不可照抄
■ 题目描述
【相对开音节】
相对开音节构成的结构为辅音+元音(aeiou)+辅音(r除外)+e,常见的单词有bike、cake等。
给定一个字符串,以空格为分隔符,反转每个单词中的字母,若单词中包含如数字等其他非字母时不进行反转。
反转后计算其中含有相对开音节结构的子串个数(连续的子串中部分字符可以重复)。
输入描述
字符串,以空格分割的多个单词,字符串长度<10000,字母只考虑小写
输出描述
含有相对开音节结构的子串个数,注:个数<10000
示例1 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
ekam a ekac
输出
2
说明
反转后为 make a cake 其中make、cake为相对开音节子串,返回2。
示例2 输入输出示例仅供调试,后台判题数据一般不包含示例
输入
!ekam a ekekac
输出
2
说明
反转后为!ekam a cakeke 因!ekam含非英文字符所以未反转,其中 cake、keke为相对开音节子串,返回2。
Java代码实现1:
import java.util.Scanner;
public class KaiYinJie {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String[] temp = sc.nextLine().split("\\s+");
System.out.println(handle(temp));
}
sc.close();
}
public static int getCount(String str) {
if (str.length() < 4) {
return 0;
}
int val = 0;
//!ekam a ekekac
if (!zimu(str)) {
for (int i = 0; i <= str.length() - 4; i++) {
if (check(str.charAt(i), str.charAt(i + 1), str.charAt(i + 2), str.charAt(i + 3))) {
val++;
}
}
} else {
for (int i = str.length() - 1; i > 2; i--) {
if (check(str.charAt(i), str.charAt(i - 1), str.charAt(i - 2), str.charAt(i - 3))) {
val++;
}
}
}
return val;
}
public static boolean check(char c1, char c2, char c3, char c4) {
if (c4 != 'e') {
return false;
}
if (c2 != 'a' && c2 != 'e' && c2 != 'i' && c2 != 'o' && c2 != 'u') {
return false;
}
if (c1 == 'a' || c1 == 'e' || c1 == 'i' || c1 == 'o' || c1 == 'u') {
return false;
}
if (!('a' <= c1 && c1 <= 'z')) {
return false;
}
if (c3 == 'a' || c3 == 'e' || c3 == 'i' || c3 == 'o' || c3 == 'u' || c3 == 'r') {
return false;
}
return 'a' <= c3 && c3 <= 'z';
}
public static int handle(String[] temp) {
int count = 0;
for (String s : temp) {
count += getCount(s);
}
return count;
}
public static boolean zimu(String s) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!('a' <= c && c <= 'z')) {
return false;
}
}
return true;
}
}
Java代码实现2:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
private static final List<Character> yuanyin = new ArrayList<>();
static {
char[] f1 = {'a', 'e', 'i', 'o', 'u'};
for (char c : f1) {
yuanyin.add(c);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int total = 0;
for (String content : input) {
boolean flag = true;
for (int j = 0; j < content.length(); j++) {
if (content.charAt(j) < 'a' || content.charAt(j) > 'z') {
flag = false;
break;
}
}
if (flag) {
total += checkKai(content);
}
}
System.out.println(total);
}
private static int checkKai(String str) {
StringBuilder strTemp = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
strTemp.append(str.charAt(i));
}
int left = 0;
int right = 0;
int total = 0;
String tem = "";
while (right <= str.length()) {
tem = strTemp.substring(left, right);
if (tem.length() < 4) {
right++;
continue;
}
if (checkTrue(tem)) {
total++;
}
left++;
right++;
}
return total;
}
private static boolean checkTrue(String str) {
if (!yuanyin.contains(str.charAt(0)) && 'e' == str.charAt(str.length() - 1)) {
int temYuan = 0;
for (int i = 1; i < str.length() - 1; i++) {
if (yuanyin.contains(str.charAt(i))) {
temYuan = i;
}
if (temYuan != 0 && !yuanyin.contains(str.charAt(i)) && str.charAt(i) != 'r' && i > temYuan) {
return true;
}
}
}
return false;
}
}
Java代码实现3:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] s = sc.nextLine().split(" ");
int len = s.length;
int count = 0;
boolean isAll;
for (String value : s) {
isAll = true;
String str = value;
int strLen = str.length();
if (strLen < 4) {
continue;
}
for (int j = 0; j < strLen; j++) {
if (!Character.isLetter(str.charAt(j))) {
isAll = false;
}
}
if (isAll) {
str = reverseStr(str);
}
for (int j = 0; j <= strLen - 4; j++) {
if (isKYJ(str.substring(j, j + 4))) {
count++;
}
}
}
System.out.println(count);
}
public static String reverseStr(String s) {
if (s.length() <= 1) {
return s;
}
return reverseStr(s.substring(1)) + s.substring(0, 1);
}
public static boolean isKYJ(String str) {
String yuanyin = "aeiou";
String s1 = String.valueOf(str.charAt(0));
String s2 = String.valueOf(str.charAt(1));
String s3 = String.valueOf(str.charAt(2));
String s4 = String.valueOf(str.charAt(3));
return !yuanyin.contains(s1)
&& yuanyin.contains(s2)
&& !yuanyin.contains(s3) && !s3.equals("r")
&& s4.equals("e");
}
}