|
| 1 | +--- |
| 2 | +id: strobogrammatic-number |
| 3 | +title: Strobogrammatic Number |
| 4 | +sidebar_label: 0246-Strobogrammatic Number |
| 5 | +tags: [Hash Map, Two Pointers] |
| 6 | +description: Solution to finding the Strobogrammatic Number in an array of strings. |
| 7 | +--- |
| 8 | + |
| 9 | +### Description |
| 10 | + |
| 11 | + |
| 12 | +A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). |
| 13 | + |
| 14 | +Write a function to determine if a number is strobogrammatic. The number is represented as a string. |
| 15 | + |
| 16 | +### Example: |
| 17 | + |
| 18 | + |
| 19 | +**Example 1:** |
| 20 | + |
| 21 | +```bash |
| 22 | +Input: "69" |
| 23 | +Output: true |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | +```bash |
| 28 | +Input: "88" |
| 29 | +Output: true |
| 30 | +``` |
| 31 | + |
| 32 | +**Example 3:** |
| 33 | +```bash |
| 34 | +Input: "962" |
| 35 | +Output: false |
| 36 | +``` |
| 37 | + |
| 38 | +### Solution |
| 39 | + |
| 40 | +#### Approach |
| 41 | + |
| 42 | +1. Create a dictionary to map each digit to its strobogrammatic counterpart. |
| 43 | +2. Iterate over the number from both ends towards the center. |
| 44 | +3. For each pair of digits, check if they are valid strobogrammatic pairs using the dictionary. |
| 45 | +4. Return true if all pairs are valid, otherwise return false. |
| 46 | + |
| 47 | + |
| 48 | +#### Codes in Different Languages |
| 49 | +<Tabs> |
| 50 | +<TabItem value="javascript" label="JavaScript"> |
| 51 | +<SolutionAuthor name="@sivaprasath"/> |
| 52 | + |
| 53 | + |
| 54 | +```javascript |
| 55 | +function isStrobogrammatic(num) { |
| 56 | + const strobogrammaticMap = { |
| 57 | + '0': '0', |
| 58 | + '1': '1', |
| 59 | + '6': '9', |
| 60 | + '8': '8', |
| 61 | + '9': '6' |
| 62 | + }; |
| 63 | + let left = 0, right = num.length - 1; |
| 64 | + while (left <= right) { |
| 65 | + if (!(num[left] in strobogrammaticMap) || strobogrammaticMap[num[left]] !== num[right]) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + left++; |
| 69 | + right--; |
| 70 | + } |
| 71 | + return true; |
| 72 | +} |
| 73 | + |
| 74 | +// Example usage: |
| 75 | +console.log(isStrobogrammatic("69")); // Output: true |
| 76 | +console.log(isStrobogrammatic("88")); // Output: true |
| 77 | +console.log(isStrobogrammatic("962")); // Output: false |
| 78 | +``` |
| 79 | + |
| 80 | +</TabItem> |
| 81 | + |
| 82 | +<TabItem value="typescript" label="TypeScript"> |
| 83 | +<SolutionAuthor name="@sivaprasath"/> |
| 84 | + |
| 85 | +```typescript |
| 86 | +function isStrobogrammatic(num: string): boolean { |
| 87 | + const strobogrammaticMap: { [key: string]: string } = { |
| 88 | + '0': '0', |
| 89 | + '1': '1', |
| 90 | + '6': '9', |
| 91 | + '8': '8', |
| 92 | + '9': '6' |
| 93 | + }; |
| 94 | + let left = 0, right = num.length - 1; |
| 95 | + while (left <= right) { |
| 96 | + if (!(num[left] in strobogrammaticMap) || strobogrammaticMap[num[left]] !== num[right]) { |
| 97 | + return false; |
| 98 | + } |
| 99 | + left++; |
| 100 | + right--; |
| 101 | + } |
| 102 | + return true; |
| 103 | +} |
| 104 | + |
| 105 | +// Example usage: |
| 106 | +console.log(isStrobogrammatic("69")); // Output: true |
| 107 | +console.log(isStrobogrammatic("88")); // Output: true |
| 108 | +console.log(isStrobogrammatic("962")); // Output: false |
| 109 | +``` |
| 110 | + |
| 111 | +</TabItem> |
| 112 | + |
| 113 | +<TabItem value="python" label="Python"> |
| 114 | +<SolutionAuthor name="@sivaprasath"/> |
| 115 | + |
| 116 | +```python |
| 117 | +def isStrobogrammatic(num): |
| 118 | + strobogrammatic_map = {'0': '0', '1': '1', '6': '9', '8': '8', '9': '6'} |
| 119 | + left, right = 0, len(num) - 1 |
| 120 | + while left <= right: |
| 121 | + if num[left] not in strobogrammatic_map or strobogrammatic_map[num[left]] != num[right]: |
| 122 | + return False |
| 123 | + left += 1 |
| 124 | + right -= 1 |
| 125 | + return True |
| 126 | + |
| 127 | +# Example usage: |
| 128 | +print(isStrobogrammatic("69")) # Output: true |
| 129 | +print(isStrobogrammatic("88")) # Output: true |
| 130 | +print(isStrobogrammatic("962")) # Output: false |
| 131 | +``` |
| 132 | +</TabItem> |
| 133 | +<TabItem value="java" label="Java"> |
| 134 | +<SolutionAuthor name="@sivaprasath"/> |
| 135 | + |
| 136 | + |
| 137 | +```java |
| 138 | +import java.util.HashMap; |
| 139 | +import java.util.Map; |
| 140 | + |
| 141 | +public class StrobogrammaticNumber { |
| 142 | + public static boolean isStrobogrammatic(String num) { |
| 143 | + Map<Character, Character> strobogrammaticMap = new HashMap<>(); |
| 144 | + strobogrammaticMap.put('0', '0'); |
| 145 | + strobogrammaticMap.put('1', '1'); |
| 146 | + strobogrammaticMap.put('6', '9'); |
| 147 | + strobogrammaticMap.put('8', '8'); |
| 148 | + strobogrammaticMap.put('9', '6'); |
| 149 | + |
| 150 | + int left = 0, right = num.length() - 1; |
| 151 | + while (left <= right) { |
| 152 | + if (!strobogrammaticMap.containsKey(num.charAt(left)) || strobogrammaticMap.get(num.charAt(left)) != num.charAt(right)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + left++; |
| 156 | + right--; |
| 157 | + } |
| 158 | + return true; |
| 159 | + } |
| 160 | + |
| 161 | + public static void main(String[] args) { |
| 162 | + System.out.println(isStrobogrammatic("69")); // Output: true |
| 163 | + System.out.println(isStrobogrammatic("88")); // Output: true |
| 164 | + System.out.println(isStrobogrammatic("962")); // Output: false |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +</TabItem> |
| 170 | +<TabItem value="cpp" label="C++"> |
| 171 | +<SolutionAuthor name="@sivaprasath"/> |
| 172 | + |
| 173 | +```cpp |
| 174 | +#include <iostream> |
| 175 | +#include <unordered_map> |
| 176 | +#include <string> |
| 177 | + |
| 178 | +using namespace std; |
| 179 | + |
| 180 | +bool isStrobogrammatic(string num) { |
| 181 | + unordered_map<char, char> strobogrammaticMap = { |
| 182 | + {'0', '0'}, |
| 183 | + {'1', '1'}, |
| 184 | + {'6', '9'}, |
| 185 | + {'8', '8'}, |
| 186 | + {'9', '6'} |
| 187 | + }; |
| 188 | + |
| 189 | + int left = 0, right = num.length() - 1; |
| 190 | + while (left <= right) { |
| 191 | + if (strobogrammaticMap.find(num[left]) == strobogrammaticMap.end() || strobogrammaticMap[num[left]] != num[right]) { |
| 192 | + return false; |
| 193 | + } |
| 194 | + left++; |
| 195 | + right--; |
| 196 | + } |
| 197 | + return true; |
| 198 | +} |
| 199 | + |
| 200 | +int main() { |
| 201 | + cout << boolalpha; |
| 202 | + cout << isStrobogrammatic("69") << endl; // Output: true |
| 203 | + cout << isStrobogrammatic("88") << endl; // Output: true |
| 204 | + cout << isStrobogrammatic("962") << endl; // Output: false |
| 205 | + return 0; |
| 206 | +} |
| 207 | +``` |
| 208 | +
|
| 209 | +</TabItem> |
| 210 | +</Tabs> |
| 211 | +
|
| 212 | +#### Explanation: |
| 213 | +<Tabs> |
| 214 | +
|
| 215 | +<TabItem value="javascript" label="Javascript"> |
| 216 | +
|
| 217 | +1. The `strobogrammaticMap` defines the valid strobogrammatic pairs. |
| 218 | +2. Two pointers, `left` and `right`, traverse the string from both ends towards the center. |
| 219 | +3. For each pair of digits, we check if they exist in the map and match the required strobogrammatic counterpart. |
| 220 | +4. If any pair is invalid, return false; if all pairs are valid, return true. |
| 221 | +
|
| 222 | +</TabItem> |
| 223 | +
|
| 224 | +<TabItem value="typescript" label="TypeScript"> |
| 225 | +
|
| 226 | +1. The `strobogrammaticMap` defines the valid strobogrammatic pairs. |
| 227 | +2. Two pointers, `left` and `right`, traverse the string from both ends towards the center. |
| 228 | +3. For each pair of digits, we check if they exist in the map and match the required strobogrammatic counterpart. |
| 229 | +4. If any pair is invalid, return false; if all pairs are valid, return true. |
| 230 | +
|
| 231 | +</TabItem> |
| 232 | +
|
| 233 | +<TabItem value="python" label="Python"> |
| 234 | +
|
| 235 | +1. The dictionary `strobogrammatic_map` defines the valid strobogrammatic pairs. |
| 236 | +2. Two pointers, `left` and `right`, are used to traverse the number from both ends. |
| 237 | +3. For each pair of digits, we check if they are in the dictionary and match the required strobogrammatic counterpart. |
| 238 | +4. If any pair is invalid, return false; if all pairs are valid, return true. |
| 239 | +
|
| 240 | +</TabItem> |
| 241 | +
|
| 242 | +<TabItem value="java" label="Java"> |
| 243 | +
|
| 244 | +1. The `strobogrammaticMap` defines the valid strobogrammatic pairs. |
| 245 | +2. Two pointers, `left` and `right`, traverse the string from both ends towards the center. |
| 246 | +3. For each pair of digits, we check if they exist in the map and match the required strobogrammatic counterpart. |
| 247 | +4. If any pair is invalid, return false; if all pairs are valid, return true. |
| 248 | +
|
| 249 | +</TabItem> |
| 250 | +
|
| 251 | +<TabItem value="cpp" label="C++"> |
| 252 | +
|
| 253 | +1. The `strobogrammaticMap` defines the valid strobogrammatic pairs. |
| 254 | +2. Two pointers, `left` and `right`, traverse the string from both ends towards the center. |
| 255 | +3. For each pair of digits, we check if they exist in the map and match the required strobogrammatic counterpart. |
| 256 | +4. If any pair is invalid, return false; if all pairs are valid, return true. |
| 257 | +
|
| 258 | +</TabItem> |
| 259 | +</Tabs> |
| 260 | +
|
| 261 | +
|
| 262 | +#### Complexity: |
| 263 | +<Tabs> |
| 264 | +
|
| 265 | +<TabItem value="javascript" label="Javascript"> |
| 266 | +
|
| 267 | +1. **Time complexity**: $O(n)$, where $n$ is the length of the input string, as we only iterate through the string once. |
| 268 | +2. **Space complexity**: $O(1)$, as we use a fixed amount of extra space for the map and pointers. |
| 269 | +3. This makes the approach efficient and suitable for checking strobogrammatic properties in linear time. |
| 270 | +
|
| 271 | +</TabItem> |
| 272 | +
|
| 273 | +<TabItem value="typescript" label="TypeScript"> |
| 274 | +
|
| 275 | +1. **Time complexity**: $O(n)$, where $n$ is the length of the input string, as we only iterate through the string once. |
| 276 | +2. **Space complexity**: $O(1)$, as we use a fixed amount of extra space for the map and pointers. |
| 277 | +3. This makes the approach efficient and suitable for checking strobogrammatic properties in linear time. |
| 278 | +
|
| 279 | +</TabItem> |
| 280 | +
|
| 281 | +<TabItem value="python" label="Python"> |
| 282 | +
|
| 283 | +1. Time complexity: $O(n)$, where $n$ is the length of the input string, as we only iterate through the string once. |
| 284 | +2. Space complexity: $O(1)$, as we use a fixed amount of extra space for the dictionary and pointers. |
| 285 | +3. This makes the approach efficient and suitable for checking strobogrammatic properties in linear time. |
| 286 | +
|
| 287 | +</TabItem> |
| 288 | +
|
| 289 | +<TabItem value="java" label="Java"> |
| 290 | +
|
| 291 | +1. **Time complexity**: $O(n)$, where $n$ is the length of the input string, as we only iterate through the string once. |
| 292 | +2. **Space complexity**: $O(1)$, as we use a fixed amount of extra space for the map and pointers. |
| 293 | +3. This makes the approach efficient and suitable for checking strobogrammatic properties in linear time. |
| 294 | +
|
| 295 | +</TabItem> |
| 296 | +<TabItem value="cpp" label="C++"> |
| 297 | +
|
| 298 | +1. **Time complexity**: $O(n)$, where $n$ is the length of the input string, as we only iterate through the string once. |
| 299 | +2. **Space complexity**: $O(1)$, as we use a fixed amount of extra space for the map and pointers. |
| 300 | +3. This makes the approach efficient and suitable for checking strobogrammatic properties in linear time. |
| 301 | +
|
| 302 | +</TabItem> |
| 303 | +</Tabs> |
| 304 | +
|
| 305 | +## References |
| 306 | +
|
| 307 | +- **LeetCode Problem:** [Strobogrammatic Number](https://leetcode.com/problems/strobogrammatic-number/) |
| 308 | +
|
| 309 | +<h2>Author:</h2> |
| 310 | +
|
| 311 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 312 | +{['sivaprasath2004'].map(username => ( |
| 313 | + <Author key={username} username={username} /> |
| 314 | +))} |
| 315 | +</div> |
0 commit comments