let s string length equal n its character number 0 n1 j integer 0ijn Lets define function f followsfsijsi1 j1rsj n1rs0 iHere sp q substre string s start position p end position q inclusive string concatenation operator rx string result write character x string reverse order if ji1 substre si1 j1 consider emptyyou give string b find value j faijb Number maximally possible if exist valid value j choose minimal j