Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a7fef69

Browse files
Add Sequential Digits solution and tests; update README with new problem link
1 parent 429c352 commit a7fef69

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
- https://leetcode.com/problems/rotate-array/description/
3636
- https://leetcode.com/problems/jump-game/description/
3737
- https://leetcode.com/problems/product-of-array-except-self/
38+
- https://leetcode.com/problems/sequential-digits/description/
3839

3940

4041

‎leetcode/1291.sequential_digits.dart

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
// https://leetcode.com/problems/sequential-digits/description/
2+
import 'package:test/test.dart';
3+
4+
void main() {
5+
final stopwatch = Stopwatch()..start();
6+
runTests();
7+
8+
print(Solution().sequentialDigits(100, 300));
9+
10+
stopwatch.stop();
11+
print('Function Execution Time : ${stopwatch.elapsedMicroseconds} micro sec');
12+
// GOOD: Function Execution Time : 000 micro sec
13+
// BAD : Function Execution Time : 000 micro sec
14+
}
15+
16+
class Solution {
17+
/// Good Solution from AI.
18+
// List<int> sequentialDigits(int low, int high) {
19+
// final List<int> result = [];
20+
21+
// for (int digitsNumber = 2; digitsNumber <= 9; digitsNumber++) {
22+
// for (int start = 1; start <= 9 - digitsNumber + 1; start++) {
23+
// int sequentialNumber = 0;
24+
// for (int j = start; j < digitsNumber + start; j++) {
25+
// sequentialNumber = 10 * sequentialNumber + j;
26+
// }
27+
28+
// if (sequentialNumber >= low && sequentialNumber <= high) {
29+
// result.add(sequentialNumber);
30+
// }
31+
// }
32+
// }
33+
34+
// return result;
35+
// }
36+
37+
/// Internet Solution (Udemy)
38+
List<int> sequentialDigits(int low, int high) {
39+
String str = "123456789";
40+
List<int> list = [];
41+
for (int i = 2; i <= 9; i++) {
42+
for (int j = 0; i + j <= 9; j++) {
43+
String tmp = str.substring(j, j + i);
44+
int val = int.parse(tmp);
45+
if (val >= low && val <= high) {
46+
list.add(val);
47+
}
48+
}
49+
}
50+
return list;
51+
}
52+
53+
/// My Solution
54+
// List<int> sequentialDigits(int low, int high) {
55+
// int digitsNumber = 2;
56+
// const String fullSequentialNumber = "123456789";
57+
// int sequentialNumber = 0;
58+
// int firstIndex = 0;
59+
60+
// final List<int> result = [];
61+
62+
// while (sequentialNumber <= high) {
63+
// sequentialNumber = int.parse(fullSequentialNumber.substring(
64+
// firstIndex,
65+
// firstIndex + digitsNumber,
66+
// ));
67+
// if (digitsNumber + firstIndex < fullSequentialNumber.length) {
68+
// ++firstIndex;
69+
// } else {
70+
// firstIndex = 0;
71+
// digitsNumber++;
72+
// }
73+
// if (sequentialNumber >= low && sequentialNumber <= high) {
74+
// result.add(sequentialNumber);
75+
// }
76+
77+
// if (digitsNumber + firstIndex > fullSequentialNumber.length) break;
78+
// }
79+
// return result;
80+
// }
81+
82+
/// My Fist Fucking Solution
83+
/// This Solution works with me from the first time
84+
// List<int> sequentialDigits(int low, int high) {
85+
// int digitsNumber = 2;
86+
// int firstSequentialNumberInDigits = 12;
87+
// int sequentialNumber = 12;
88+
// final List<int> result = [];
89+
90+
// while (sequentialNumber <= high) {
91+
// if (sequentialNumber >= low) {
92+
// result.add(sequentialNumber);
93+
// }
94+
95+
// int mod = sequentialNumber % 10;
96+
97+
// /// Check are we going with same number of digits or we are going to add another digit
98+
// if (mod != 9) {
99+
// int newSequentialNumber = 0;
100+
101+
// /// Get the next sequentialNumber form the before --> will be reversed 321
102+
// /// current sequentail 123 -> 432
103+
// while (sequentialNumber != 0) {
104+
// mod = sequentialNumber % 10;
105+
// sequentialNumber ~/= 10;
106+
// newSequentialNumber = newSequentialNumber * 10 + mod + 1;
107+
// }
108+
// sequentialNumber = newSequentialNumber;
109+
// newSequentialNumber = 0;
110+
111+
// /// Reverse the number from 321 to 123
112+
// while (sequentialNumber != 0) {
113+
// mod = sequentialNumber % 10;
114+
// sequentialNumber ~/= 10;
115+
// newSequentialNumber = newSequentialNumber * 10 + mod;
116+
// }
117+
// sequentialNumber = newSequentialNumber;
118+
// } else {
119+
// int mod = firstSequentialNumberInDigits % 10;
120+
// firstSequentialNumberInDigits =
121+
// firstSequentialNumberInDigits * 10 + mod + 1;
122+
// sequentialNumber = firstSequentialNumberInDigits;
123+
// digitsNumber++;
124+
// if (digitsNumber > 9) {
125+
// break;
126+
// }
127+
// }
128+
// }
129+
130+
// return result;
131+
// }
132+
}
133+
134+
void runTests() {
135+
final Solution s = Solution();
136+
137+
group('Sequential Digits', () {
138+
// Basic examples from problem statement
139+
test('Example 1: low=100, high=300 → [123,234]', () {
140+
expect(s.sequentialDigits(100, 300), equals([123, 234]));
141+
});
142+
143+
test(
144+
'Example 2: low=1000, high=13000 → [1234,2345,3456,4567,5678,6789,12345]',
145+
() {
146+
expect(s.sequentialDigits(1000, 13000),
147+
equals([1234, 2345, 3456, 4567, 5678, 6789, 12345]));
148+
});
149+
150+
// Edge cases
151+
test('Single digit range: low=1, high=9 → []', () {
152+
expect(s.sequentialDigits(1, 9), equals([]));
153+
});
154+
155+
test('Range with no sequential digits: low=100, high=110 → []', () {
156+
expect(s.sequentialDigits(100, 110), equals([]));
157+
});
158+
159+
test('Range equals single sequential digit: low=123, high=123 → [123]', () {
160+
expect(s.sequentialDigits(123, 123), equals([123]));
161+
});
162+
163+
// Small ranges
164+
test('Small range with one result: low=10, high=50 → [12,23,34,45]', () {
165+
expect(s.sequentialDigits(10, 50), equals([12, 23, 34, 45]));
166+
});
167+
168+
test(
169+
'Small range with multiple results: low=100, high=500 → [123,234,345,456]',
170+
() {
171+
expect(s.sequentialDigits(100, 500), equals([123, 234, 345, 456]));
172+
});
173+
174+
// Large ranges
175+
test(
176+
'Large range: low=10, high=1000000000 → [12,23,34,45,56,67,78,89,123,...789,1234,...6789,...123456789]',
177+
() {
178+
final result = s.sequentialDigits(10, 1000000000);
179+
expect(result.length, equals(36));
180+
expect(result.first, equals(12));
181+
expect(result.last, equals(123456789));
182+
});
183+
184+
// Boundary cases
185+
test('Minimum constraints: low=10, high=10 → []', () {
186+
expect(s.sequentialDigits(10, 10), equals([]));
187+
});
188+
189+
test('Maximum constraints: low=10, high=1000000000 → full sequence', () {
190+
expect(s.sequentialDigits(10, 1000000000).length, equals(36));
191+
});
192+
193+
// Special cases
194+
test(
195+
'Range starting with sequential digit: low=123, high=500 → [123,234,345,456]',
196+
() {
197+
expect(s.sequentialDigits(123, 500), equals([123, 234, 345, 456]));
198+
});
199+
200+
test(
201+
'Range ending with sequential digit: low=100, high=678 → [123,234,345,456,567,678]',
202+
() {
203+
expect(
204+
s.sequentialDigits(100, 678), equals([123, 234, 345, 456, 567, 678]));
205+
});
206+
207+
// All possible lengths
208+
test(
209+
'All 2-digit sequential numbers: low=10, high=100 → [12,23,34,45,56,67,78,89]',
210+
() {
211+
expect(s.sequentialDigits(10, 100),
212+
equals([12, 23, 34, 45, 56, 67, 78, 89]));
213+
});
214+
215+
test(
216+
'All 3-digit sequential numbers: low=100, high=1000 → [123,234,345,456,567,678,789]',
217+
() {
218+
expect(s.sequentialDigits(100, 1000),
219+
equals([123, 234, 345, 456, 567, 678, 789]));
220+
});
221+
});
222+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /