Hello,
I would like to check a string contains "hello" or not?
In the following string if we remove some characters we have "hello".
ahhellllloou
sdfhwesflrfgvloouhhlh
But the following string is not matched with "hello".
hlelo
I wrote a program and it works fine but Do we have a simple way? for example with regex?
Thanks
package newpackage; import java.util.*; public class NewMain { public static void main(String[] args) { Scanner input = new Scanner(System.in); String myString = input.next().toLowerCase(); char[] myChar = new char[myString.length()]; char[] finalChar = new char[5]; int j = 0; for (int i = 0; i < myString.length(); i++) { if ((myString.charAt(i) == 'h') || (myString.charAt(i) == 'e') || (myString.charAt(i) == 'l') || (myString.charAt(i) == 'o')) myChar[j++] = myString.charAt(i); } int countH =0, countE = 0, countL = 0, countO = 0; int FcountH =1, FcountE = 1, FcountL = 2, FcountO = 1; for (int i = 0; i < myChar.length; i++) { if (myChar[i] == 'h') countH++; else if (myChar[i] == 'e') countE++; else if (myChar[i] == 'l') countL++; else if (myChar[i] == 'o') countO++; } j = 0; for (int i = 0; i < myChar.length; i++) { if (myChar[i] == 'h') { if (countH > 1 && FcountH == 1){ finalChar[j++] = 'h'; countH--; FcountH = 0; } else if (countH == 1 && FcountH == 1) { finalChar[j++] = 'h'; FcountH = 0; } } else if (myChar[i] == 'e' && FcountE == 1) { if (countE > 1){ finalChar[j++] = 'e'; countE--; FcountE = 0; } else if (countE == 1 && FcountE == 1){ finalChar[j++] = 'e'; FcountE = 0; } } else if (myChar[i] == 'l' ) { if (countL > 2 && FcountL >= 1){ finalChar[j++] = 'l'; countL--; FcountL--; } else if (countL >=1 && FcountL != 0){ finalChar[j++] = 'l'; countL--; FcountL--; } } else if (myChar[i] == 'o' && FcountO == 1) { if (countO > 1){ finalChar[j++] = 'o'; countO--; FcountO = 0; } else if (countO == 1 && FcountO == 1){ finalChar[j++] = 'o'; FcountO = 0; } } } String finalString = new String(finalChar); if (finalString.equals("hello")) System.out.println("YES"); else System.out.println("NO"); } }