Skip to content

feat: PalindromeNumber Solution Added #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/PalindromeNumber/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Palindrome Number

LeetCode [source](https://leetcode.com/problems/palindrome-number/)

## Solution

### Intuition

The challenge with this problem is that you're not allowed to use any string. You have to take the integer and come up with a solution that doesn't employ string or any of its methods. From the first glance, what was apparent is that we'll need a data structure to hold the individual integers for comparison.

### Algorithm
Details of the the algorithm for the solution is as follows:
1. If it's a single digit, then it's a Palindrome Number.
2. Ignore any negative integers.
3. For all other types of integers, pop each digit and store them in a vector in order.
4. Compare the `(n + i)th` integer with `(vector.size() - 1 - i)th integer.
5. If this comparisos fails (i.e. `comparison == false`) return false.
6. Else continue the comparisons until `i` reaches the middle of the vector. If no `false` was return before, it means all the comparisons were `true` and therefore, the integer is a palindrome.

### Complexity Analysis
* Time Complexity: `// TODO`
* Space Complexity: `// TODO`

### AC Result

`11509 / 11509 test cases passed`

| Status | Runtime | Memory |
|--------|---------|--------|
| Accepted | 36 ms | 11.7 MB |
68 changes: 68 additions & 0 deletions src/PalindromeNumber/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Solution by Imtiaz Ahmed (Github: tiazahmd)

#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
bool isPalindrome(int x) {
if (x >= 0 && x < 10) {
return true;
}
else if (x >= 10) {
vector<int> vec;
long div = 10;
long ct = 1;
int num = 0;
int digCount = 0;
int temp = x;

while (temp != 0) {
temp /= 10;
digCount++;
}

for (int i = 0; i < digCount; i++) {
num = x % div;
num /= ct;
div *= 10;
ct *= 10;
vec.push_back(num);
}

for (int i = 0; i < vec.size(); i++) {
if (i == vec.size() - 1) return true;
else if (vec[i] != vec[vec.size() - 1 - i]) return false;
}

}
else {
return false;
}

return false;
};
};

int stringToInteger(string input) {
return stoi(input);
}

string boolToString(bool input) {
return input ? "True" : "False";
}

int main() {
string line;
while (getline(cin, line)) {
int x = stringToInteger(line);

bool ret = Solution().isPalindrome(x);

string out = boolToString(ret);
cout << out << endl;
}
return 0;
}