Return true if the given string contains 1 or 3 ‘e’ chars. Otherwise return false.





Following are the test cases

Test cases Result Status
stringE(Hello) true OK
stringE(Heelele) false OK
stringE(Heelle) true OK

Java solution


package test;

public class Program16 {
	public static void main(String[] args) {
		Program16.stringE("Hello");
		Program16.stringE("Heelele");
		Program16.stringE("Heelle");
	}
	public static boolean stringE(String str) {
		char[] letters = str.toCharArray();
		int count = 0;
		for (int i = 0; i < letters.length; i++) {
			if (letters[i] == 'e') {
				count++;
			}
		}
		if (count == 1 || count == 3) {
			return true;
		}
		return false;
	}
}

Javascript solution


const stringE = (str) => {
    let count = 0;
    for (let i = 0; i < str.length; i++) {
        if (str[i] === 'e') {
            count++;
        }
    }
    if (count === 1 || count === 3) {
        return true;
    }
    return false;
}

console.log(stringE('Hello')); //true
console.log(stringE('Heelele')); //false
console.log(stringE('Heelle')); //true




About the author

Bushan Sirgur

Well, I am Bushan Sirgur from Banglore, India. Currently, I am working as a Software Developer in a Service Base Company. I am interested in JAVA/J2EE, Angular 2, JavaScript, jQuery, MongoDB.

View all posts

Leave a Reply

Your email address will not be published. Required fields are marked *