Skip to content

Commit 9a8b2b1

Browse files
committed
day 8
1 parent 8e0b128 commit 9a8b2b1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Super Palindromes
3+
=================
4+
5+
Let's say a positive integer is a super-palindrome if it is a palindrome, and it is also the square of a palindrome.
6+
7+
Given two positive integers left and right represented as strings, return the number of super-palindromes integers in the inclusive range [left, right].
8+
9+
Example 1:
10+
Input: left = "4", right = "1000"
11+
Output: 4
12+
Explanation: 4, 9, 121, and 484 are superpalindromes.
13+
Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.
14+
15+
Example 2:
16+
Input: left = "1", right = "2"
17+
Output: 1
18+
19+
Constraints:
20+
1 <= left.length, right.length <= 18
21+
left and right consist of only digits.
22+
left and right cannot have leading zeros.
23+
left and right represent integers in the range [1, 1018].
24+
left is less than or equal to right.
25+
*/
26+
27+
class Solution
28+
{
29+
public:
30+
bool isPalendrome(long long &n)
31+
{
32+
string st = to_string(n);
33+
int i = 0, j = st.size() - 1;
34+
while (i < j)
35+
{
36+
if (st[i++] != st[j--])
37+
return false;
38+
}
39+
return true;
40+
}
41+
42+
int superpalindromesInRange(string l, string r)
43+
{
44+
int ans = 0, MAGIC = pow(10, 5);
45+
long long left = 0, right = 0;
46+
for (auto &i : l)
47+
left = left * 10 + (i - '0');
48+
for (auto &i : r)
49+
right = right * 10 + (i - '0');
50+
51+
for (int k = 1; k < MAGIC; ++k)
52+
{
53+
long long R = 0;
54+
string S = to_string(k);
55+
for (int i = S.size() - 2; i >= 0; --i)
56+
S += S[i];
57+
for (auto &i : S)
58+
R = R * 10 + (i - '0');
59+
R = R * R;
60+
if (R > right)
61+
break;
62+
if (R >= left && isPalendrome(R))
63+
ans++;
64+
}
65+
66+
for (int k = 1; k < MAGIC; ++k)
67+
{
68+
long long R = 0;
69+
string S = to_string(k);
70+
for (int i = S.size() - 1; i >= 0; --i)
71+
S += S[i];
72+
for (auto &i : S)
73+
R = R * 10 + (i - '0');
74+
R = R * R;
75+
if (R > right)
76+
break;
77+
if (R >= left && isPalendrome(R))
78+
ans++;
79+
}
80+
81+
return ans;
82+
}
83+
};

Leetcode Daily Challenge/May-2021/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@
99
| 5. | [Jump Game II](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3732/) | [cpp](./05.%20Jump%20Game%20II.cpp) |
1010
| 6. | [Convert Sorted List to Binary Search Tree](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3733/) | [cpp](./06.%20Convert%20Sorted%20List%20to%20Binary%20Search%20Tree.cpp) |
1111
| 7. | [Delete Operation for Two Strings](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge-2021/598/week-1-may-1st-may-7th/3734/) | [cpp](./07.%20Delete%20Operation%20for%20Two%20Strings.cpp) |
12+
| 8. | [Super Palindromes](https://leetcode.com/problems/super-palindromes/) | [cpp](./08.%20Super%20Palindromes.cpp) |

Striver Sheet/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@
141141
- [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) - [Cpp Soultion](./Day-15/Longest%20Common%20Prefix.cpp)
142142
- [Rabin-Karp Algorithm for Pattern Searching](https://www.geeksforgeeks.org/rabin-karp-algorithm-for-pattern-searching/)
143143

144+
### Day 16 ()
145+
146+
- []() - [Cpp Soultion](./Day-16/.cpp)
147+
- []() - [Cpp Soultion](./Day-16/.cpp)
148+
- []() - [Cpp Soultion](./Day-16/.cpp)
149+
- []() - [Cpp Soultion](./Day-16/.cpp)
150+
- []() - [Cpp Soultion](./Day-16/.cpp)
151+
- []() - [Cpp Soultion](./Day-16/.cpp)
152+
144153
###
145154

146155
- []() - [Cpp Soultion](./Day-/.cpp)

0 commit comments

Comments
 (0)