-
Notifications
You must be signed in to change notification settings - Fork 0
/
2396. Strictly Palindromic Number.cpp
116 lines (84 loc) · 2.32 KB
/
2396. Strictly Palindromic Number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 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
// Accepted
// 37.7K
// Submissions
// 42.9K
// Acceptance Rate
// 87.7%
class Solution {
public:
bool isPalindrome(vector<int>& check)
{
for (int i =0, j = check.size()-1; i<=check.size()/2; ++i, --j)
{
if (check[i] != check [j])
return false;
}
return true;
}
vector <int> BaseConverter(int num, int base)
{
vector <int> result;
int t = 0;
while(num)
{
result.push_back(num%base);
num/=base;
if(num<base)
{
result.push_back(num);
break;
}
}
// cout<<endl<<"Before Reversing : ";
// for(int i = 0; i<result.size(); ++i)
// {
// cout<<result[i]<<" ";
// }
// cout<<endl<<"After reversing: ";
for (int i =0, j = result.size()-1; i<result.size()/2; ++i, --j)
{
cout<<result[i]<<" "<<result[j]<<endl;
t = result[i];
result[i] = result[j];
result[j] = t;
}
// for(int i = 0; i<result.size(); ++i)
// {
// cout<<result[i]<<" ";
// }
return result;
}
bool isStrictlyPalindromic(int n) {
bool flag = true;
vector <int> response;
for(int i = 2; i<=n-2; ++i)
{
cout<<"Checking for "<<i<<": ";
response = BaseConverter(n,i);
flag = isPalindrome(response);
if (!flag){
cout<<"Not Strictly palindrome";
// return false;
break;
}
}
return flag;
}
};