publicclassSolution {publicstaticintnumberOfPalindromicSubstrings(String S) {// Conversion of string into required formchar[] str =newchar[2*S.length() +3]; str[0] ='@'; str[1] ='#'; str[str.length-1] ='$';int index =2;for (char c :S.toCharArray()) { str[index++] = c; str[index++] ='#'; }int[] palindromicLength =newint[str.length];int center =0, right =0;for (int i =1; i <palindromicLength.length-1; ++i) {// Finding mirror elementint mirror = i -2* (i - c);// If i is within rangeif (i < right) palindromicLength[i] =Math.min(right - i, palindromicLength[mirror]);// Expanding about center, using any min length we havewhile (str[i + palindromicLength[i] +1] == str[i - palindromicLength[i] -1]) palindromicLength[i]++;// If range of palindrome of i, is greater than range of "main" palindromeif (i + palindromicLength[i] > right) { center = i; right = i + palindromicLength[i]; } }int ans =0;for (int length : palindromicLength) ans += (length +1) /2;return ans; }}