APR
BAN USERchar [] str = inputWord.toCharArray();
char[] alpha = {'h','i','r','e'};
int M = 4;int t=0, i=0, count=0, j=0;
int[] countArr = new int[M];
for(t = 0, i = 0, count = 0, j=0; i < str.length ; i++) {
if( str[i] == alpha[t % M]) count++;
else if(str[i] == alpha[++t %M] ) {countArr[j %M]=count; count=1; j++;}
else return false;
}
countArr[j%M]=count;
for (int k=0;k<M-1;k++) {
if (countArr[k]==countArr[k+1]) continue; else return false;
}
return (t%M == M-1) ? true : false;
private static boolean isvalidWord(String word) {
char [] wordChar = word.toCharArray();
int hCounter = 0, iCounter = 0, rCounter=0,eCounter=0;
boolean hVisitFlag=false,iVisitFlag=false,rVisitFlag=false,eVisitFlag=false ;
boolean wordMismatch = false;
for (int i=0; i< wordChar.length; i++) {
switch (wordChar[i]) {
case 'h':
if(!hVisitFlag)
if(hCounter != iCounter || hCounter != rCounter || hCounter != eCounter) {
wordMismatch = true; break;
}
hCounter++;
hVisitFlag=true; iVisitFlag=false;rVisitFlag=false;eVisitFlag=false; break;
case 'i':
iCounter++;
if(!iVisitFlag && !hVisitFlag) {
wordMismatch = true; break;
}
iVisitFlag=true; hVisitFlag=false;rVisitFlag=false;eVisitFlag=false; break;
case 'r':
rCounter++;
if(!rVisitFlag && !iVisitFlag) {
wordMismatch = true; break;
}
rVisitFlag=true; iVisitFlag=false;hVisitFlag=false;eVisitFlag=false; break;
case 'e':
eCounter++;
if(!eVisitFlag && !rVisitFlag){
wordMismatch = true; break;
}
eVisitFlag=true; iVisitFlag=false;rVisitFlag=false;hVisitFlag=false; break;
}
if(wordMismatch) return !wordMismatch;
}
return true;
}
I guess it might need few improvements, not tested yet
- APR January 02, 2014