Skip to content

Commit 1be5cdd

Browse files
committed
premium leetcode
1 parent b695773 commit 1be5cdd

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
9393
- [Largest Perimeter Triangle](LargestPerimeterTriangle/largest_perimeter_triangle.dart)
9494
- [Binary Tree Paths](BinaryTreePaths/binary_tree_paths.dart)
9595
- [Delete Node in a Linked List](DeleteNodeInALinkedList/delete_node_in_a_linked_list.dart)
96+
- [Strobo-Grammatic Number](StrobogrammaticNumber/strobogrammatic_number.dart)
9697

9798
## Reach me via
9899

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
3+
4+
-* Strobo-Grammatic Number *-
5+
6+
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
7+
8+
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
9+
10+
For example, the numbers "69", "88", and "818" are all strobogrammatic.
11+
12+
*/
13+
14+
import 'dart:collection';
15+
16+
class Solution {
17+
bool isStrobogrammatic(String nums) {
18+
HashMap<String, String> dict = HashMap();
19+
dict['0'] = '0';
20+
dict['1'] = '1';
21+
dict['8'] = '8';
22+
dict["6"] = '9';
23+
dict['9'] = '6';
24+
int i = 0;
25+
int j = nums.length - 1;
26+
while (i <= j) {
27+
int f = nums.codeUnitAt(i);
28+
int b = nums.codeUnitAt(j);
29+
if (dict.containsKey(f) && dict.containsKey(b) && dict[f] == b) {
30+
i++;
31+
j--;
32+
} else {
33+
return false;
34+
}
35+
}
36+
return true;
37+
}
38+
}
39+
40+
class B {
41+
bool isStrobogrammatic(String nums) {
42+
final Map<String, String> lut = {
43+
'0': "0",
44+
'1': '1',
45+
'6': '9',
46+
'8': '8',
47+
'9': '6'
48+
};
49+
for (int l = 0, r = nums.length - 1; l <= r; l++, r--) {
50+
if (lut.containsKey(nums[l]) ==
51+
lut.entries.toList().getRange(lut.length - 2, lut.length) ||
52+
lut[nums[l]] != nums[r]) {
53+
return false;
54+
}
55+
}
56+
return true;
57+
}
58+
}
59+
60+
class C {
61+
bool isStrobogrammatic(String nums) {
62+
for (int i = 0, j = nums.length - 1; i <= j; i++, j--)
63+
if (!"00 11 88 696".contains(
64+
nums[i] + "" + nums[i],
65+
)) return false;
66+
return true;
67+
}
68+
}
69+
70+
class D {
71+
bool isStrobogrammatic(String nums) {
72+
HashMap<String, String> map = HashMap();
73+
map['0'] = '0';
74+
map['1'] = '1';
75+
map['8'] = '8';
76+
map["6"] = '9';
77+
map['9'] = '6';
78+
if (nums.length == 0) return false;
79+
int left = 0, right = nums.length - 1;
80+
while (left <= right) {
81+
int c = nums.codeUnitAt(left);
82+
if (!map.containsKey(c) || map[c] != nums.codeUnitAt(right)) return false;
83+
left++;
84+
right--;
85+
}
86+
return true;
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# 🔥 Strobo-Grammatic Number 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Explanation
4+
5+
1. For the first test case if we rotate “69” by 180 degrees, it’s the same.
6+
7+
2. For the second test case if we rotate “692” by 180 degrees, it’s not the same number.
8+
9+
## Approach Idea
10+
11+
- We need to determine what each digit becomes when rotated by 180 degrees. There are three possibilities for each digit:
12+
- it becomes invalid
13+
- it stays the same
14+
- it becomes a different digit
15+
- We’ll consider a digit to be rotatable if, and only if, that digit becomes a valid digit when rotated. If we think carefully, we can identify that 0, 1, 6, 8, 9 are rotatable as only these digits are valid when it is rotated upside down by 180 degrees. So if the number contains any digit other than these, we can easily say that it is not a strobogrammatic number. For other digits, we need to check if their rotation is the same as the number at its counterpart.
16+
- Let N be the length of the input string.
17+
18+
### Time complexity: O(N)
19+
20+
For each of the N digits in the string, we’re doing a single lookup and comparison.
21+
22+
### Space complexity: O(1)
23+
24+
We are only using constant extra space. This is an in-place algorithm.
25+
26+
## Solution - 1
27+
28+
```dart
29+
import 'dart:collection';
30+
31+
class Solution {
32+
bool isStrobogrammatic(String nums) {
33+
HashMap<String, String> dict = HashMap();
34+
dict['0'] = '0';
35+
dict['1'] = '1';
36+
dict['8'] = '8';
37+
dict["6"] = '9';
38+
dict['9'] = '6';
39+
int i = 0;
40+
int j = nums.length - 1;
41+
while (i <= j) {
42+
int f = nums.codeUnitAt(i);
43+
int b = nums.codeUnitAt(j);
44+
if (dict.containsKey(f) && dict.containsKey(b) && dict[f] == b) {
45+
i++;
46+
j--;
47+
} else {
48+
return false;
49+
}
50+
}
51+
return true;
52+
}
53+
}
54+
```
55+
56+
## Solution - 2
57+
58+
```dart
59+
class Solution {
60+
bool isStrobogrammatic(String nums) {
61+
final Map<String, String> lut = {
62+
'0': "0",
63+
'1': '1',
64+
'6': '9',
65+
'8': '8',
66+
'9': '6'
67+
};
68+
for (int l = 0, r = nums.length - 1; l <= r; l++, r--) {
69+
if (lut.containsKey(nums[l]) ==
70+
lut.entries.toList().getRange(lut.length - 2, lut.length) ||
71+
lut[nums[l]] != nums[r]) {
72+
return false;
73+
}
74+
}
75+
return true;
76+
}
77+
}
78+
```
79+
80+
## Solution - 3
81+
82+
```dart
83+
class Solution {
84+
bool isStrobogrammatic(String nums) {
85+
for (int i = 0, j = nums.length - 1; i <= j; i++, j--)
86+
if (!"00 11 88 696".contains(
87+
nums[i] + "" + nums[i],
88+
)) return false;
89+
return true;
90+
}
91+
}
92+
```
93+
94+
## Solution - 4
95+
96+
```dart
97+
import 'dart:collection';
98+
99+
class Solution {
100+
bool isStrobogrammatic(String nums) {
101+
HashMap<String, String> map = HashMap();
102+
map['0'] = '0';
103+
map['1'] = '1';
104+
map['8'] = '8';
105+
map["6"] = '9';
106+
map['9'] = '6';
107+
if (nums.length == 0) return false;
108+
int left = 0, right = nums.length - 1;
109+
while (left <= right) {
110+
int c = nums.codeUnitAt(left);
111+
if (!map.containsKey(c) || map[c] != nums.codeUnitAt(right)) return false;
112+
left++;
113+
right--;
114+
}
115+
return true;
116+
}
117+
}
118+
```
119+
120+
## Disclaimer:-
121+
122+
- First of all i don't have a premium subscription
123+
- Second of all this code is in DART language but you will get the general idea how to implement in other language
124+
125+
## [GitHub Link](https://github.com/ayoubzulfiqar/leetcode)

0 commit comments

Comments
 (0)