304 North Cardinal St.
Dorchester Center, MA 02124

# Strictly Palindromic Number LeetCode Solution

## Problem – Strictly Palindromic Number LeetCode Solution

An integer `n` is strictly palindromic if, for every base `b` between `2` and `n - 2` (inclusive), the string representation of the integer `n` in base `b` is palindromic.

Given an integer `n`, return `true` if `n` is strictly palindromic and `false` otherwise.

A string is palindromic if it reads the same forward and backward.

Example 1:

``````Input: n = 9
Output: false
Explanation: In base 2: 9 = 1001 (base 2), which is palindromic.
In base 3: 9 = 100 (base 3), which is not palindromic.
Therefore, 9 is not strictly palindromic so we return false.
Note that in bases 4, 5, 6, and 7, n = 9 is also not palindromic.
``````

Example 2:

``````Input: n = 4
Output: false
Explanation: We only consider base 2: 4 = 100 (base 2), which is not palindromic.
Therefore, we return false.

``````

Constraints:

• `4 <= n <= 105`

## Strictly Palindromic Number LeetCode Solution in C++

``````class Solution {
public:
bool isStrictlyPalindromic(int n)
{
int tmp;                      // store n into a temp val so actual n will not affected
string base;                  // for storing the value of each base (2 to n-2)

for(int i=2;i<=n-2;i++)
{
tmp=n;

while(tmp)                                   // calculating the base i for value tmp
{
base = base + to_string(tmp%i);
tmp = tmp/i;
}

int st=0;
int en=base.size()-1;
while(st<en)                                // check for palindrom
{
if(base[st++]!=base[en--]) return false;
}

base.clear();                               // clear base each time after iteration
}
return true;
}
};
``````

## Strictly Palindromic Number LeetCode Solution in Java

``````class Solution {
public boolean isStrictlyPalindromic(int n) {

// toString(int i, int radix) method of Integer class
// i: the int value that we want to convert
// the radix or base to be used in getting the string representation of int method argument

for(int i=2;i<=n-2;i++){
if( !checkPalindrome(Integer.toString(n,i)) )
return false;
}
return true;
}

public boolean checkPalindrome(String s){

for(int i=0;i<s.length()/2;i++){

if( !(s.charAt(i) == s.charAt(s.length()-i-1)) )
return false;
}
return true;
}
}

``````

## Strictly Palindromic Number LeetCode Solution in Python

``````class Solution:
def isStrictlyPalindromic(self, n: int) -> bool:
for b in range(2, n-1):
rep = []
k = n
while k > 0:
rep.append(k%b)
k //= b
if rep != rep[::-1]: return False
return True
``````
##### Strictly Palindromic Number LeetCode Solution Review:

In our experience, we suggest you solve this Strictly Palindromic Number LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Strictly Palindromic Number LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Strictly Palindromic Number LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions