![JAR search and dependency download from the Maven repository](/logo.png)
g2301_2400.s2396_strictly_palindromic_number.Solution.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-kotlin Show documentation
Show all versions of leetcode-in-kotlin Show documentation
Kotlin-based LeetCode algorithm problem solutions, regularly updated
package g2301_2400.s2396_strictly_palindromic_number
// #Medium #Math #Two_Pointers #Brainteaser #2023_07_02_Time_130_ms_(58.06%)_Space_32.8_MB_(74.19%)
class Solution {
fun isStrictlyPalindromic(n: Int): Boolean {
for (i in 2..n - 2) {
val num = Integer.toString(i)
val s = baseConversion(num, 10, i)
if (!checkPalindrome(s)) {
return false
}
}
return true
}
private fun baseConversion(number: String, sBase: Int, dBase: Int): String {
// Parse the number with source radix
// and return in specified radix(base)
return Integer.toString(number.toInt(sBase), dBase)
}
private fun checkPalindrome(s: String): Boolean {
var start = 0
var end = s.length - 1
while (start <= end) {
if (s[start] != s[end]) {
return false
}
start++
end--
}
return true
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy