[InterviewBit] Palindrome Numbers

Palindrome Numbers

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool pal(int r) {
string s = to_string(r);
int i = 0, j = s.length() - 1;
while(i < j) {
if(s[i] != s[j]) return false;
i++,j--;
}
return true;
}
int Solution::solve(int A, int B, int C) {
int res = 0, now = 0, l = A, r = A;
while(r < B) {
while(r <= B and r - l <= C) {
if(pal(r)) now++;
if(r - l <= C) res = max(res, now);
r++;
}
while(l < r and r - l > C) {
if(pal(l)) now--;
l++;
}
}
return res;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/11/21/PS/interviewbit/palindrome-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.