educative.io

A better solution using two pointers

        int p1 = 0;
	int p2 = 1;
	boolean isSegment = false;
	
	while(p2 != inputString.length()) {
		String sub = inputString.substring(p1, p2+1);
		if(dictionary.contains(sub)) {
			isSegment = true;
			p1 = p2 + 1;
			p2++;
		}
		else {
			isSegment = false;
			p2++;
		}
	}
	
	return isSegment;`

Course: https://www.educative.io/courses/coderust-hacking-the-coding-interview
Lesson: Word Break Problem - Coderust: Hacking the Coding Interview

1 Like

Hi @Prakhar_Shukla

Thank you for the suggestion. It is great seeing our users thinking out of the box and coming with an alternate solution. Your feedback is much appreciated!