1. 题目
输入一串英文单词,不同单词之间用一个或多个空格""隔开,统计该串单词中包含e”的单词数量,e"不区分大小写,要求时间复杂度小于等于O(n).
2. 代码
- js实现
let str = 'Nice to meet you'
function getNumOfE(str) {
let arr = str.split(' ')
let num = 0
for(let i = 0; i < arr.length; i++) {
if(arr[i].lastIndexOf('e') > -1 || arr[i].lastIndexOf('E') > -1) {
num++
}
}
return num
}
console.log(getNumOfE(str))
- java实现
public class HelloWorld {
public static void main(String []args) {
String s = "Nice to meet you";
String[] s2 = s.split(" ");
int count = 0;
for (String i : s2) {
if( i.contains("e") || i.contains("E"))
count++;
/*
for(int j = 0; j < i.length(); j++){
if( i.charAt(j) == 'e' || i.charAt(j) == 'E'){
count++;
//flag = true;
//break;
}*/
}
System.out.println(count);
}
}