题目大意:输入的字符串中若存在有序“hello”即输出YES,否则NO。该"hello"不需要一定连续。
解题思路:使用字符数组{'h','e','l','l','o'},进行字符串扫描,若匹配成功,则对应的flag[i]=true,下次循环head=++j,并且跳出内层循环。只有当五个flag[i]均为true时才满足YES,否则NO。
注意事项:无
以下为解题代码(java实现)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
char[] str = string.toCharArray();
char[] ch = {'h','e','l','l','o'};
boolean[] flag = {false,false,false,false,false};
int head = 0;
for(int i = 0; i < ch.length;i++){
for(int j = head;j < string.length();j++){
if(ch[i] == str[j]){
flag[i] = true;
head = ++j;
break;
}
}
}
if(flag[0] && flag[1] &&flag[2] && flag[3] && flag[4]){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}