Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jun 8, 2021

Resolves #57

Algorithm:

We could think of a strobogrammatic number as another kind palindrome.
From the question statement, we already know the length of this number. So, in order to build this number, we can create a character array. In this char array, we put the reflective digits in the opposite sides of the array.

For example, in an array of length 5, if we put 1 on the index 0, we put another one on the index 4 (n-1). After that, we put a 6 on the index 1 (previous left+1) and then the corresponding digit 9 on the index 3 (n-2, previous right+1). At the end, when we continue the pattern both left and right becomes 2. We then put digits that are suitable for a middle reflection (0, 1, 8).
This pattern also holds when modified a bit, for even lengths:

At even lengths such as 4, left will eventually assume the index value 2 and right the index value 1 (left>right). Then, we shall not add anything into the character array.

Only thing we should also consider is that we shouldn't place '0' at the front when left is 0, that would not be a valid number.

We can also put the reflective number pairs in char[][] and easily loop over them.

import java.util.ArrayList;
import java.util.List;

public class StrobogrammaticNumber2 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great:
image

Copy link
Collaborator

@altay9 altay9 Jun 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quote of Erdem's explanation:

Resolves #57

Algorithm:

We could think of a strobogrammatic number as another kind palindrome.
From the question statement, we already know the length of this number. So, in order to build this number, we can create a character array. In this char array, we put the reflective digits in the opposite sides of the array.

For example, in an array of length 5, if we put 1 on the index 0, we put another one on the index 4 (n-1). After that, we put a 6 on the index 1 (previous left+1) and then the corresponding digit 9 on the index 3 (n-2, previous right+1). At the end, when we continue the pattern both left and right becomes 2. We then put digits that are suitable for a middle reflection (0, 1, 8).
This pattern also holds when modified a bit, for even lengths:

At even lengths such as 4, left will eventually assume the index value 2 and right the index value 1 (left>right). Then, we shall not add anything into the character array.

Only thing we should also consider is that we shouldn't place '0' at the front when left is 0, that would not be a valid number.

We can also put the reflective number pairs in char[][] and easily loop over them.

Copy link
Collaborator

@altay9 altay9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nice explanation that and a clean code that expresses itself.
Thanks, Erdem.

@altay9 altay9 modified the milestone: 100/170 Jun 8, 2021
@ErdemT09
Copy link
Collaborator Author

ErdemT09 commented Jun 8, 2021

code that expresses itself

I think writing good variable, method etc. names is better than writing extensive comments. With methods, once can additionally modularize the code too.

@ErdemT09 ErdemT09 merged commit 560d91b into master Jun 8, 2021
@ErdemT09 ErdemT09 deleted the 247.-Strobogrammatic-Number-II branch June 8, 2021 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

247. Strobogrammatic Number II

3 participants