Current String (Length: 5)
🏰
PREFIX Castle
Prefixes start from index 0. They are the beginning portions of the
string.
🏚️
SUFFIX Dungeon
Suffixes end at index N-1. They are the ending portions of the
string.
BOSS FIGHT: Find the LPS
Find the Longest Prefix that is also a Suffix (excluding the full string)
Select matching prefix and suffix to defeat the boss!
Gate Locked - Match prefix with suffix
🎯 Challenge: Pick the Valid Prefix
Which of these is a valid prefix of the current string?
💡 Prefix & Suffix Generators
// Generate all prefixes (start from index 0) function
getAllPrefixes(str) { const prefixes = []; for (let i = 1; i <=
str.length; i++) { prefixes.push(str.substring(0, i)); } return
prefixes; } // Generate all suffixes (end at index N-1) function
getAllSuffixes(str) { const suffixes = []; for (let i = 0; i <
str.length; i++) { suffixes.push(str.substring(i)); } return suffixes;
} // Find longest proper prefix which is also suffix function
findLPS(str) { const n = str.length; for (let len = n - 1; len > 0;
len--) { const prefix = str.substring(0, len); const suffix =
str.substring(n - len); if (prefix === suffix) { return prefix; } }
return ""; // No proper prefix-suffix match }