Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2396.html

2396. Strictly Palindromic Number

  • Difficulty: Medium.
  • Related Topics: Math, Two Pointers, Brainteaser.
  • Similar Questions: Palindrome Number, Stone Game.

Problem

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

Solution (Java, C++, Python)

  • class Solution {
      // every base from 2 to n-1 of integer n
      // must be a palindrome. This is pretty unlikely
      public boolean isStrictlyPalindromic(int n) {
        for(int i = 2; i < n-1; i++) {
          // Integer.toString(int n, int i) converts n to base i
          if(!isPalindrome(Integer.toString(n, i))) {
            return false;
          }
        }
        
        return true;
      }
      
      private boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length()-1;
        while(left < right) {
          if(str.charAt(left) != str.charAt(right)) {
            return false;
          }
          left++;
          right--;
        }
        
        return true;
      }
    }
    
    ############
    
    class Solution {
        public boolean isStrictlyPalindromic(int n) {
            return false;
        }
    }
    
  • class Solution:
        def isStrictlyPalindromic(self, n: int) -> bool:
            return False
    
    ############
    
    # 2396. Strictly Palindromic Number
    # https://leetcode.com/problems/strictly-palindromic-number/
    
    class Solution:
        def isStrictlyPalindromic(self, n: int) -> bool:
            
            def numberToBase(n, b):
                digits = []
                
                while n:
                    digits.append(n % b)
                    n //= b
                    
                return "".join(map(str, digits[::-1]))
            
            for k in range(2, n - 2 + 1):
                s = numberToBase(n, k)
                
                if s != s[::-1]:
                    return False
                
            return True
    
    
  • class Solution {
    public:
        bool isStrictlyPalindromic(int n) {
            return false;
        }
    };
    
  • func isStrictlyPalindromic(n int) bool {
    	return false
    }
    
  • function isStrictlyPalindromic(n: number): boolean {
        return false;
    }
    
    
  • impl Solution {
        pub fn is_strictly_palindromic(n: i32) -> bool {
            false
        }
    }
    
    

Explain:

nope.

Complexity:

  • Time complexity : O(n).
  • Space complexity : O(n).

All Problems

All Solutions