We had some 2-dimensional coordinates, like (1, 3) or (2, 0.5). Then, we removed all commas, decimal points, and spaces and ended up with the string s.
For example, (1, 3) becomes s = "(13)" and (2, 0.5) becomes s = "(205)".
Return a list of strings representing all possibilities for what our original coordinates could have been.
Our original representation never had extraneous zeroes, so we never started with numbers like "00", "0.0", "0.00", "1.0", "001", "00.01", or any other number that can be represented with fewer digits. Also, a decimal point within a number never occurs without at least one digit occurring before it, so we never started with numbers like ".1".
The final answer list can be returned in any order. All coordinates in the final answer have exactly one space between them (occurring after the comma).
💡 Note:Split "123" at positions 1 and 2. For split "1|23": (1, 23) and (1, 2.3). For split "12|3": (12, 3) and (1.2, 3). All combinations are valid.
Example 2 — Leading Zero Constraint
$Input:s = "(00011)"
›Output:["(0.001, 1)", "(0, 0.011)"]
💡 Note:Split positions create parts with leading zeros. "0001" cannot be "0001" or "000.1" (invalid), but "0.001" is valid. "1" is valid, "0011" becomes "0.011".
The key insight is to systematically split the string and validate number formats. Numbers cannot have leading zeros (except single '0'), cannot end with '0' after decimal, and must have digits before decimal point. Best approach uses enumeration to generate all valid combinations. Time: O(n³), Space: O(n)
Common Approaches
✓
Optimized
⏱️ Time: N/A
Space: N/A
Backtracking with Pruning
⏱️ Time: O(n³)
Space: O(n)
Use backtracking to generate coordinate combinations by first splitting the string, then recursively trying decimal placements. Prune invalid branches early to avoid unnecessary computation.
Brute Force Enumeration
⏱️ Time: O(n³)
Space: O(n)
Generate all possible ways to split the string into two parts, then for each part generate all valid decimal number representations. Combine valid pairs into coordinate format.
Algorithm Steps — Algorithm Steps
Code -
solution.c — C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
static char results[10000][100];
static int resultCount = 0;
static char validNumbers[1000][50];
static int validCount = 0;
bool isValidInteger(const char* s, int len) {
if (len == 0) return false;
if (len == 1) return true;
return s[0] != '0';
}
bool isValidDecimal(const char* left, int leftLen, const char* right, int rightLen) {
if (leftLen == 0 || rightLen == 0) return false;
if (leftLen > 1 && left[0] == '0') return false;
if (right[rightLen - 1] == '0') return false;
return true;
}
void getValidNumbers(const char* s, int len) {
validCount = 0;
if (len == 0) return;
if (isValidInteger(s, len)) {
strncpy(validNumbers[validCount], s, len);
validNumbers[validCount][len] = '\0';
validCount++;
}
for (int i = 1; i < len; i++) {
if (isValidDecimal(s, i, s + i, len - i)) {
strncpy(validNumbers[validCount], s, i);
validNumbers[validCount][i] = '.';
strncpy(validNumbers[validCount] + i + 1, s + i, len - i);
validNumbers[validCount][len + 1] = '\0';
validCount++;
}
}
}
int compareStrings(const void* a, const void* b) {
return strcmp((const char*)a, (const char*)b);
}
void ambiguousCoordinates(const char* s) {
int len = strlen(s);
char cleaned[100];
strncpy(cleaned, s + 1, len - 2);
cleaned[len - 2] = '\0';
int n = strlen(cleaned);
resultCount = 0;
for (int i = 1; i < n; i++) {
char left[50], right[50];
strncpy(left, cleaned, i);
left[i] = '\0';
strncpy(right, cleaned + i, n - i);
right[n - i] = '\0';
getValidNumbers(left, strlen(left));
char leftNums[1000][50];
int leftCount = validCount;
for (int j = 0; j < validCount; j++) {
strcpy(leftNums[j], validNumbers[j]);
}
getValidNumbers(right, strlen(right));
char rightNums[1000][50];
int rightCount = validCount;
for (int j = 0; j < validCount; j++) {
strcpy(rightNums[j], validNumbers[j]);
}
for (int l = 0; l < leftCount; l++) {
for (int r = 0; r < rightCount; r++) {
sprintf(results[resultCount], "(%s, %s)", leftNums[l], rightNums[r]);
resultCount++;
}
}
}
qsort(results, resultCount, sizeof(results[0]), compareStrings);
}
int main() {
char line[1000];
fgets(line, sizeof(line), stdin);
int len = strlen(line);
if (line[len - 1] == '\n') {
line[len - 1] = '\0';
len--;
}
ambiguousCoordinates(line);
printf("[");
for (int i = 0; i < resultCount; i++) {
printf("\"%s\"", results[i]);
if (i < resultCount - 1) printf(",");
}
printf("]\n");
return 0;
}
Time & Space Complexity
Time Complexity
⏱️
n
2n
⚠ Quadratic Growth
Space Complexity
n
2n
⚡ Linearithmic Space
32.0K Views
MediumFrequency
~25 minAvg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡Explanation
AI Ready
💡 SuggestionTabto acceptEscto dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen
Algorithm Visualization
Pinch to zoom • Tap outside to close
Test Cases
0 passed
0 failed
3 pending
Select Compiler
Choose a programming language
Compiler list would appear here...
AI Editor Features
Header Buttons
💡
Explain
Get a detailed explanation of your code. Select specific code or analyze the entire file. Understand algorithms, logic flow, and complexity.
🔧
Fix
Automatically detect and fix issues in your code. Finds bugs, syntax errors, and common mistakes. Shows you what was fixed.
💡
Suggest
Get improvement suggestions for your code. Best practices, performance tips, and code quality recommendations.
💬
Ask AI
Open an AI chat assistant to ask any coding questions. Have a conversation about your code, get help with debugging, or learn new concepts.
Smart Actions (Slash Commands)
🔧
/fix Enter
Find and fix issues in your code. Detects common problems and applies automatic fixes.
💡
/explain Enter
Get a detailed explanation of what your code does, including time/space complexity analysis.
🧪
/tests Enter
Automatically generate unit tests for your code. Creates comprehensive test cases.
📝
/docs Enter
Generate documentation for your code. Creates docstrings, JSDoc comments, and type hints.
⚡
/optimize Enter
Get performance optimization suggestions. Improve speed and reduce memory usage.
AI Code Completion (Copilot-style)
👻
Ghost Text Suggestions
As you type, AI suggests code completions shown in gray text. Works with keywords like def, for, if, etc.
Tabto acceptEscto dismiss
💬
Comment-to-Code
Write a comment describing what you want, and AI generates the code. Try: # two sum, # binary search, # fibonacci
💡
Pro Tip: Select specific code before using Explain, Fix, or Smart Actions to analyze only that portion. Otherwise, the entire file will be analyzed.