question_slug
stringlengths
3
77
title
stringlengths
1
183
slug
stringlengths
12
45
summary
stringlengths
1
160
author
stringlengths
2
30
certification
stringclasses
2 values
created_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
updated_at
stringdate
2013-10-25 17:32:12
2025-04-12 09:38:24
hit_count
int64
0
10.6M
has_video
bool
2 classes
content
stringlengths
4
576k
upvotes
int64
0
11.5k
downvotes
int64
0
358
tags
stringlengths
2
193
comments
int64
0
2.56k
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java (Using Queue | simple dfs)
java-using-queue-simple-dfs-by-dhruvpanw-ennc
First approach:(Using a queue)\n1. Make a boolean array hasReached[] where hasReached[i] means ith vertex can now reach 0.\n2. Push all edges in a queue\n3. Pop
dhruvpanwar31
NORMAL
2021-08-13T21:32:26.651543+00:00
2021-08-13T21:32:26.651585+00:00
458
false
First approach:(Using a queue)\n1. Make a boolean array hasReached[] where hasReached[i] means ith vertex can now reach 0.\n2. Push all edges in a queue\n3. Pop queue, see if in the edge (u,v) we have a node that reaches 0.( hasReached[u] or hasReached[v] ) \n4. Let the node that reaches 0 be \'u\'.\n5. So now for \'v\...
4
0
['Depth-First Search', 'Queue', 'Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C++ | DFS with explanation
c-dfs-with-explanation-by-xpharry-38vt
Assume each city can reach the city 0, then from the city 0, it can always reach all the cities if the connections are all reversed. Then we can traverse this r
xpharry
NORMAL
2021-03-09T23:15:53.069256+00:00
2021-03-09T23:15:53.069289+00:00
331
false
Assume each city can reach the city 0, then from the city 0, it can always reach all the cities if the connections are all reversed. Then we can traverse this reversed directed-graph and check each connection without repeatation, we can get the minimum number of edges to change by counting the connection that can be fo...
4
1
[]
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java || BFS || O(n)
java-bfs-on-by-legendarycoder-8hkb
\n public int minReorder(int n, int[][] connections) {\n int count = 0;\n\t\tList[] graph = new ArrayList[n];\n\t\tfor (int i = 0; i < n; i++)\n\t\t\t
LegendaryCoder
NORMAL
2021-02-20T06:48:17.723022+00:00
2021-02-20T06:48:17.723051+00:00
285
false
\n public int minReorder(int n, int[][] connections) {\n int count = 0;\n\t\tList<Integer>[] graph = new ArrayList[n];\n\t\tfor (int i = 0; i < n; i++)\n\t\t\tgraph[i] = new ArrayList<Integer>();\n\n\t\tfor (int[] connection : connections) {\n\t\t\tgraph[connection[0]].add(connection[1]);\n\t\t\tgraph[connect...
4
0
[]
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java 100%, simplified topological sort, no tree/graph needed
java-100-simplified-topological-sort-no-fa2wg
Simplified topological sort, keep iterate cities that can be reached by flipping direction until all cities can be reached.\n\nclass Solution {\n public int
tao68
NORMAL
2021-01-04T22:41:43.470108+00:00
2021-01-04T22:41:43.470146+00:00
302
false
Simplified topological sort, keep iterate cities that can be reached by flipping direction until all cities can be reached.\n```\nclass Solution {\n public int minReorder(int n, int[][] connections) {\n boolean[] reach = new boolean[n]; // this array tracks cities reachable so far\n reach[0] = true; //...
4
0
[]
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Javascript BFS, easy to understand.
javascript-bfs-easy-to-understand-by-and-4jd9
Thinking process:\nSince the target is to minimize the number of road to reverse so that all cities can reach city 0, then for those directly connected to 0, sa
andyoung
NORMAL
2020-10-03T22:28:30.287882+00:00
2020-10-03T22:58:45.890791+00:00
721
false
**Thinking process:**\nSince the target is to minimize the number of road to reverse so that all cities can reach city `0`, then for those directly connected to `0`, say `nodes` , if it\'s already in the right direction, no change; if otherwise, reverse it. Same idea applies to the remaining cities that connected to `n...
4
0
['Breadth-First Search', 'JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[Python] 7-line DFS solution
python-7-line-dfs-solution-by-m-just-2n03
python\ndef minReorder(self, n: int, connections: List[List[int]]) -> int:\n edges = collections.defaultdict(list)\n for a, b in connections:\n edg
m-just
NORMAL
2020-09-21T19:51:19.182805+00:00
2020-09-21T19:51:19.182837+00:00
425
false
```python\ndef minReorder(self, n: int, connections: List[List[int]]) -> int:\n edges = collections.defaultdict(list)\n for a, b in connections:\n edges[a].append((b, 1))\n edges[b].append((a, 0))\n\n def dfs(i, p):\n return sum(d + dfs(j, i) for j, d in edges[i] if j != p)\n\n return d...
4
0
['Depth-First Search', 'Python']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python Beats 100% with Explanation (Better than Editorial)
python-beats-100-with-explanation-better-p63j
Approach: BFS/Greedy Search with a Seen SetProblem Breakdown You are givenncities labeled from0ton-1and a list ofconnections, whereconnections[i] = [a, b]repres
jxmils
NORMAL
2025-02-19T14:20:00.915629+00:00
2025-02-19T14:20:00.915629+00:00
744
false
### `Approach: BFS/Greedy Search with a Seen Set` #### `Problem Breakdown` - You are given `n` cities labeled from `0` to `n-1` and a list of `connections`, where `connections[i] = [a, b]` represents a directed road from city `a` to city `b`. - You must reorder some roads so that **every city can reach city `0`**. - R...
3
0
['Python3']
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python, beats 87%, clean solution with recursion
python-beats-87-clean-solution-with-recu-wsot
IntuitionDespite directioned edge, we should setup graph with biderectional edges, with marking current direction. Setup recursion, which will consider conectio
dev_lvl_80
NORMAL
2025-02-03T06:43:51.289009+00:00
2025-02-03T06:43:51.289009+00:00
488
false
# Intuition Despite directioned edge, we should setup graph with biderectional edges, with marking current direction. Setup recursion, which will consider conection existance and current direction # Approach <!-- Describe your approach to solving the problem. --> # Complexity - Time complexity: O(N) - Space complex...
3
0
['Python3']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy to Understand simple Bfs beats 100% O(n)
easy-to-understand-simple-bfs-beats-100-en6f0
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
hemanth2-ko
NORMAL
2024-10-23T17:02:25.007594+00:00
2024-10-23T17:02:25.007631+00:00
240
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:...
3
0
['C++']
3
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Solution By Dare2Solve | Detailed Explanation | Clean Code
solution-by-dare2solve-detailed-explanat-59kg
Explanation []\nauthorslog.com/blog/a89c2S0zHB\n\n# Code\n\ncpp []\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n
Dare2Solve
NORMAL
2024-08-22T08:44:24.649503+00:00
2024-08-22T08:44:24.649532+00:00
1,265
false
```Explanation []\nauthorslog.com/blog/a89c2S0zHB\n```\n# Code\n\n```cpp []\nclass Solution {\npublic:\n int minReorder(int n, vector<vector<int>>& connections) {\n vector<vector<int>> grid(n);\n vector<int> vis(n, 0);\n\n for (const auto& conn : connections) {\n int src = conn[0];\n ...
3
1
['Breadth-First Search', 'Graph', 'Python', 'C++', 'Java', 'Python3', 'JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java Level-Order Traversal Solution Using BFS with Detailed Explanations
java-level-order-traversal-solution-usin-bxqr
Intuition\nWhen should we change the direction of a route? If the route starts from a city close to city 0 and ends at a city far from city 0, we need to change
Michael-Qu
NORMAL
2024-06-17T22:38:51.968529+00:00
2024-06-17T22:38:51.968550+00:00
287
false
# Intuition\nWhen should we change the direction of a route? If the route starts from a city close to city 0 and ends at a city far from city 0, we need to change its direction since it brings people "away" from city 0. Otherwise we don\'t need to change its direction since it already meets our need. In this case, once...
3
0
['Tree', 'Breadth-First Search', 'Graph', 'Queue', 'Java']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Python | BFS | DFS | Graphs | Easy to Understand
python-bfs-dfs-graphs-easy-to-understand-zlw9
Approach: Create a graph with both original edges and their reversed counterparts (with scores 1 and 0, respectively). Traverse the graph starting from node \'
Coder1918
NORMAL
2024-04-01T20:52:22.027053+00:00
2024-04-01T21:37:17.015638+00:00
933
false
**Approach:** Create a graph with both original edges and their reversed counterparts (with scores 1 and 0, respectively). Traverse the graph starting from node \'0\', incrementing a count for each original edge that points away from node \'0\'. This count represents the necessary reorientations.\nBelow code provides ...
3
0
['Depth-First Search', 'Breadth-First Search', 'Graph', 'Python3']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Breadth-first search
breadth-first-search-by-burkh4rt-dnfi
Approach\nFirst create a lookup dictionary adj that maps vertices to adjacent/contiguous edges. Initialize 0 as the current vertex set curr and the set seen of
burkh4rt
NORMAL
2024-01-28T15:02:28.931059+00:00
2024-01-28T15:02:28.931094+00:00
911
false
# Approach\nFirst create a lookup dictionary `adj` that maps vertices to adjacent/contiguous edges. Initialize 0 as the current vertex set `curr` and the set `seen` of vertices that have been visited. Iteratively update `curr` by adding vertices adjacent to the current set if they have not already been seen. Edges `e` ...
3
0
['Breadth-First Search', 'Python3']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Reorder Nodes
reorder-nodes-by-noob_1012-q88m
\n/**\n * @param {number} n\n * @param {number[][]} connections\n * @return {number}\n */\nvar minReorder = function(n, connections) {\n let visitSet = new S
noob_1012
NORMAL
2024-01-08T14:13:58.358096+00:00
2024-01-08T14:14:21.137845+00:00
151
false
```\n/**\n * @param {number} n\n * @param {number[][]} connections\n * @return {number}\n */\nvar minReorder = function(n, connections) {\n let visitSet = new Set();\n let count = 0;\n let neighbours = new Map();\n let edges = new Set();\n for(let i=0;i<n;i++){\n neighbours[i] = [];\n }\n \n...
3
0
['Depth-First Search', 'JavaScript']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
[C] DFS, graph
c-dfs-graph-by-leetcodebug-im4d
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
leetcodebug
NORMAL
2023-07-17T15:36:33.281927+00:00
2023-07-17T15:36:33.281977+00:00
24
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here...
3
0
['Depth-First Search', 'Graph', 'C']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Java easy with bfs
java-easy-with-bfs-by-mohd_danish_khan-g019
Approach\ncreate a map {city : neighbours} and set of all the edges\n\nnow do a bfs and check on every city if there is an edge (neigh, city) in our edge set, i
Mohd_Danish_Khan
NORMAL
2023-07-13T21:53:39.227780+00:00
2023-07-13T21:53:39.227797+00:00
632
false
# Approach\ncreate a map {city : neighbours} and set of all the edges\n\nnow do a bfs and check on every city if there is an edge (neigh, city) in our edge set, if its not their increase the changes count. and move on to next city. keep visited set to avoid checking the already checked node.\n\n# Code\n```\nclass Solut...
3
0
['Java']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Solution in C++
solution-in-c-by-ashish_madhup-x8ch
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
ashish_madhup
NORMAL
2023-03-24T15:16:22.745508+00:00
2023-03-24T15:16:22.745532+00:00
203
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
reorder routes to make all paths lead to the city zero
reorder-routes-to-make-all-paths-lead-to-e7eu
Intuition\nstore all given connections in a adjaceny list smartly (in pair form) such that given connections can be checked and created connection can be checke
nobita_n0bi
NORMAL
2023-03-24T13:56:23.061090+00:00
2023-03-24T13:56:23.061127+00:00
340
false
# Intuition\nstore all given connections in a adjaceny list smartly (in pair form) such that given connections can be checked and created connection can be checked at the time of dfs function working\n\n# Approach\nnow let\'s talk about complete approach i stored all given connection in a adjaency list of pairs such th...
3
0
['Greedy', 'Graph', 'C++']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Easy understand graph problems(C++)
easy-understand-graph-problemsc-by-vikas-z7nr
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
vikash_2003
NORMAL
2023-03-24T12:35:21.127995+00:00
2023-03-24T12:35:21.128048+00:00
6,118
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['C++']
1
reorder-routes-to-make-all-paths-lead-to-the-city-zero
Using BFS C++ Easy Solution
using-bfs-c-easy-solution-by-kisna-i69m
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time
kisna
NORMAL
2023-03-24T12:01:53.872379+00:00
2023-03-24T12:01:53.872410+00:00
888
false
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
0
['Breadth-First Search', 'C++']
0
reorder-routes-to-make-all-paths-lead-to-the-city-zero
C# DFS
c-dfs-by-dmitriy-maksimov-13sq
Approach\nTreat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge.\n\n# Comp
dmitriy-maksimov
NORMAL
2023-03-24T10:39:52.431748+00:00
2023-03-24T10:39:52.431779+00:00
667
false
# Approach\nTreat the graph as undirected. Start a dfs from the root, if you come across an edge in the forward direction, you need to reverse the edge.\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(n)$$\n\n# Code\n```\npublic class Solution\n{\n private readonly Dictionary<int, List<(int cit...
3
0
['C#']
2
reorder-routes-to-make-all-paths-lead-to-the-city-zero
dfs
dfs-by-mr_stark-3nhb
\nclass Solution {\npublic:\n \n void dfs(vector<vector<pair<int,int>>> &g,int s, int &cnt,vector<bool> &v)\n {\n if(v[s]){\n return
mr_stark
NORMAL
2023-03-24T08:46:27.338388+00:00
2023-03-24T08:50:22.777067+00:00
275
false
```\nclass Solution {\npublic:\n \n void dfs(vector<vector<pair<int,int>>> &g,int s, int &cnt,vector<bool> &v)\n {\n if(v[s]){\n return ;\n }\n v[s] = true;\n \n for(auto &i: g[s])\n { \n if(!v[i.first]){\n cnt+=i.second;\n ...
3
0
['C']
0
smallest-string-with-a-given-numeric-value
[Java/C++] Easiest Possible Exaplained!!
javac-easiest-possible-exaplained-by-hi-4imxc
How\'s going Ladies - n - Gentlemen, today we are going to solve another coolest problem i.e. Smallest String With A Given Numeric Value\n\nWhat the problem is
hi-malik
NORMAL
2022-03-22T00:54:32.589593+00:00
2022-03-22T01:01:25.144052+00:00
7,304
false
How\'s going Ladies - n - Gentlemen, today we are going to solve another coolest problem i.e. **Smallest String With A Given Numeric Value**\n\nWhat the problem is saying, we have to generate the **string** with **numeric sum** equals to `k` & have the `n` characters\n\nLet\'s understand with an example,\n\n**Input**: ...
220
7
[]
30
smallest-string-with-a-given-numeric-value
[C++] Simple O(N) with explanation
c-simple-on-with-explanation-by-cgsgak1-rjhq
The idea is simple:\n1) Build the string of length k, which consists of letter \'a\' (lexicographically smallest string).\n2) Increment string from right to lef
cgsgak1
NORMAL
2020-11-22T04:01:59.788252+00:00
2021-03-18T20:13:00.454231+00:00
6,930
false
The idea is simple:\n1) Build the string of length k, which consists of letter \'a\' (lexicographically smallest string).\n2) Increment string from right to left until it\'s value won\'t reach the target.\n\nTime complexity O(n), space complexity O(1) - no additional space used except for result string.\n```\nclass Sol...
131
10
['C', 'C++']
13
smallest-string-with-a-given-numeric-value
🌻|| C++|| Easy Approach || PROPER EXPLANATION
c-easy-approach-proper-explanation-by-ab-kik0
ALGORITHM\n\nStep - 1 Initialize a string s with n number of \'a\' to make it as a smallest possible lexiographic.\nStep - 2 Count of \'a = 1\' , so if i have
Abhay_Rautela
NORMAL
2022-03-22T01:24:48.597520+00:00
2022-03-22T01:24:48.597550+00:00
7,437
false
### ALGORITHM\n```\nStep - 1 Initialize a string s with n number of \'a\' to make it as a smallest possible lexiographic.\nStep - 2 Count of \'a = 1\' , so if i have n number of \'a\' it means it sum is also \'n\'.\nStep - 3 Reduce the value of k by n (i.e k=k-n);\nStep - 4 Start traversing the string from the end bec...
88
1
['C']
15
smallest-string-with-a-given-numeric-value
C++ Reverse Fill
c-reverse-fill-by-votrubac-iazt
Generate the initial string with all \'a\' characters. This will reduce k by n. \n \nThen, turn rightmost \'a\' into \'z\' (\'a\' + 25, or \'a\' + k) while k
votrubac
NORMAL
2020-11-22T04:13:25.815258+00:00
2020-11-22T06:21:44.391096+00:00
3,634
false
Generate the initial string with all \'a\' characters. This will reduce `k` by `n`. \n \nThen, turn rightmost \'a\' into \'z\' (\'a\' + 25, or \'a\' + k) while `k` is positive.\n\n```cpp\nstring getSmallestString(int n, int k) {\n string res = string(n, \'a\');\n k -= n;\n while (k > 0) {\n res[--n] ...
85
7
[]
10
smallest-string-with-a-given-numeric-value
✔️ [Python3] GREEDY FILLING (🌸¬‿¬), Explained
python3-greedy-filling-_-explained-by-ar-itri
UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.\n\nSince we are forming the lexicographically smallest string,
artod
NORMAL
2022-03-22T02:12:40.544744+00:00
2022-03-22T03:22:21.950923+00:00
4,360
false
**UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nSince we are forming the lexicographically smallest string, we just simply fill our result with `a`s ( o\u02D8\u25E1\u02D8o). But hold on, that result will not necessarily have the required score c(\uFF9F.\uFF9F*) U...
66
2
['Python3']
7
smallest-string-with-a-given-numeric-value
✅ C++ || Dry Run || Easy || Simple & Short || O(N)
c-dry-run-easy-simple-short-on-by-knockc-vmd3
1663. Smallest String With A Given Numeric Value\nKNOCKCAT\n\n\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Lexi
knockcat
NORMAL
2022-03-22T00:47:14.032205+00:00
2022-12-31T07:04:57.128016+00:00
2,027
false
# 1663. Smallest String With A Given Numeric Value\n**KNOCKCAT**\n\n```\n1. Easy C++\n2. Line by Line Explanation with Comments.\n3. Detailed Explanation \u2705\n4. Lexicographically smallest string of length n and sum k with Dry Run.\n5. Please Upvote if it helps\u2B06\uFE0F\n```\n``` ```\n[LeetCode](http://github.com...
65
2
['String', 'C']
9
smallest-string-with-a-given-numeric-value
[Java/Python 3] Two O(n) codes w/ brief explanation and analysis.
javapython-3-two-on-codes-w-brief-explan-v67l
Mehtod 1: Greedily reversely place values\n\n1. Make sure each value of the n characters is at least 1: initialized all as \'a\';\n2. Put as more value at the
rock
NORMAL
2020-11-22T04:25:28.789229+00:00
2022-03-22T12:50:20.922441+00:00
4,650
false
**Mehtod 1: Greedily reversely place values**\n\n1. Make sure each value of the n characters is at least `1`: initialized all as `\'a\'`;\n2. Put as more value at the end of the String as possible.\n```java\n public String getSmallestString(int n, int k) {\n k -= n;\n char[] ans = new char[n];\n ...
65
1
['Java', 'Python3']
9
smallest-string-with-a-given-numeric-value
[Python] O(n) math solution, explained
python-on-math-solution-explained-by-dba-yiis
In this problem we need to construct lexicographically smallest string with given properties, and here we can use greedy strategy: try to take as small symbol a
dbabichev
NORMAL
2021-01-28T08:59:35.172473+00:00
2021-01-28T08:59:35.172508+00:00
2,378
false
In this problem we need to construct **lexicographically smallest string** with given properties, and here we can use **greedy** strategy: try to take as small symbol as possible until we can. Smallest letter means `a`, so our string in general case will look like this:\n\n`aa...aa?zz...zz`,\n\nwhere some of the groups...
59
8
['Greedy']
5
smallest-string-with-a-given-numeric-value
✅ Explained with Comments | Easy | 90% | O(n) | JS
explained-with-comments-easy-90-on-js-by-uetk
\n/* \nIdea is to build the string from the end.\n1. We create an array (numericArr) to store string (1 indexed) corresponding to it\'s \nnumeric value as given
_kapi1
NORMAL
2022-03-22T02:09:30.664841+00:00
2022-03-22T02:09:30.664883+00:00
6,684
false
```\n/* \nIdea is to build the string from the end.\n1. We create an array (numericArr) to store string (1 indexed) corresponding to it\'s \nnumeric value as given i.e [undefined,a,b,c,......,z] \n\nNow we have to fill n places i.e _ _ _ _ _ _ (Think it as fill in the blanks)\n\nWe will fill this from the end. The hig...
26
1
['JavaScript']
11
smallest-string-with-a-given-numeric-value
C++ Simple and Short O(n) Solution, faster than 97%
c-simple-and-short-on-solution-faster-th-pu91
At first, we will initialize the resulting string in the target size with \'a\'s.\nSo we used \'n\' out of the target \'k\' - we can reduce n from k.\nThen, we
yehudisk
NORMAL
2021-01-28T09:05:10.113896+00:00
2021-01-28T15:58:12.696838+00:00
1,469
false
At first, we will initialize the resulting string in the target size with \'a\'s.\nSo we used \'n\' out of the target \'k\' - we can reduce n from k.\nThen, we go from left to right and while k > 0 we turn the \'a\'s to \'z\'s, then there might be a middle character with some letter that equals to the leftover of k.\nO...
26
4
['C']
4
smallest-string-with-a-given-numeric-value
Python without any loop
python-without-any-loop-by-jaguary-wskm
\n num2 = (k-n) // 25\n if num2 == n: return \'z\' * n\n\n num1 = n - num2 - 1\n num = k - (num1 + num2 * 26)\n return \'a\'
JaguarY
NORMAL
2020-11-22T04:11:52.431191+00:00
2020-11-25T08:04:15.130111+00:00
1,616
false
```\n num2 = (k-n) // 25\n if num2 == n: return \'z\' * n\n\n num1 = n - num2 - 1\n num = k - (num1 + num2 * 26)\n return \'a\' * num1 + chr(num+96) + \'z\' * num2\n```\nBasically, this result will start with a bunch of "a"(num1) and end with a bunch of "z"(num2), with at most one oth...
24
4
[]
7
smallest-string-with-a-given-numeric-value
[Python] Simple solution - Greedy - O(N) [Explanation + Code + Comment]
python-simple-solution-greedy-on-explana-qmjr
The lexicographically smallest string of length n could be \'aaa\'.. of length n\nThe constraint is that the numeric value must be k.\nTo satisfy this constrain
mihirrane
NORMAL
2020-11-22T04:17:33.309872+00:00
2021-01-29T00:24:38.358491+00:00
1,504
false
The lexicographically smallest string of length n could be \'aaa\'.. of length n\nThe constraint is that the numeric value must be k.\nTo satisfy this constraint, we will greedily add either \'z\' or character form of (k - value) to the string.\n\nFor eg. \nn = 3, k = 32\nInitially, ans = \'aaa\', val = 3, i =2\nSince ...
19
0
['Greedy', 'Python', 'Python3']
2
smallest-string-with-a-given-numeric-value
[C++, Python, Javascript] Easy Solution w/ Explanation | beats 100% / 100%
c-python-javascript-easy-solution-w-expl-74zj
(Note: This is part of a series of Leetcode solution explanations (index). If you like this solution or find it useful, please upvote this post.)\n\n---\n\nIdea
sgallivan
NORMAL
2021-01-28T09:33:38.406672+00:00
2021-02-03T05:07:07.182952+00:00
882
false
*(Note: This is part of a series of Leetcode solution explanations ([**index**](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you like this solution or find it useful,* ***please upvote*** *this post.)*\n\n---\n\n***Idea:***\n\nThe basic idea is simple: in order for the string to be as lexigraphicall...
16
0
['C', 'Python', 'JavaScript']
3
smallest-string-with-a-given-numeric-value
Java Simple Solution with explanation
java-simple-solution-with-explanation-by-4eaa
Imaging an array with length n and value between 1-26 representing the character values\nFill all slots with minimumr equirement a, then we have value k-a left\
htttth
NORMAL
2020-11-22T04:02:26.863151+00:00
2020-11-22T18:26:00.795485+00:00
1,164
false
Imaging an array with length n and value between 1-26 representing the character values\nFill all slots with minimumr equirement a, then we have value k-a left\nstart filling slots up to 26 (\'z\') fromt he end of the array, we can calculate with:\nnumber of \'z\' = k%25\na single character with remaining value of k%2...
14
5
[]
7
smallest-string-with-a-given-numeric-value
✅[Python] Only 2 Lines Solution || O(1) || Greedy
python-only-2-lines-solution-o1-greedy-b-0rrv
\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n z, alp = divmod(k-n-1, 25)\n return (n-z-1)*\'a\'+chr(ord(\'a\')+alp+
subinium
NORMAL
2022-03-22T00:47:14.219204+00:00
2022-03-22T00:49:50.425814+00:00
1,248
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n z, alp = divmod(k-n-1, 25)\n return (n-z-1)*\'a\'+chr(ord(\'a\')+alp+1)+z*\'z\'\n```
12
0
['Greedy', 'Python']
2
smallest-string-with-a-given-numeric-value
[Python3] Runtime: 24 ms, faster than 99.60% | Memory: 14.8 MB, less than 95.18%
python3-runtime-24-ms-faster-than-9960-m-z0pe
\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n diff = k - n\n q = diff // 25\n r = diff % 25\n ans = "
anubhabishere
NORMAL
2022-03-22T05:18:17.558364+00:00
2022-03-22T05:18:17.558390+00:00
737
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n diff = k - n\n q = diff // 25\n r = diff % 25\n ans = "a"*(n-q-1) + chr(97+r) + "z"*qt if r else "a"*(n-q)+ "z"*q\n return ans\n```
9
0
['Math', 'Greedy', 'Python']
0
smallest-string-with-a-given-numeric-value
Python - very easy to follow - O(N) - Char Dict
python-very-easy-to-follow-on-char-dict-6dxao
\n\ndef getSmallestString(self, n: int, k: int) -> str:\n\tc_dict = {i+1: chr(ord(\'a\') + i) for i in range(26)} # {1:\'a\', 2:\'b\' ... 26:\'z\'}\n\n\tlst =
vvvirenyu
NORMAL
2021-01-28T09:05:03.384979+00:00
2021-01-28T09:12:53.323592+00:00
403
false
\n```\ndef getSmallestString(self, n: int, k: int) -> str:\n\tc_dict = {i+1: chr(ord(\'a\') + i) for i in range(26)} # {1:\'a\', 2:\'b\' ... 26:\'z\'}\n\n\tlst = [\'\']*n\n \n\tfor i in range(n-1, -1, -1): # Reverse fill\n\t\tc = min(26, k-i) # if k-i > 26, character is \'z\' else c_dict[k-i]\n\t\tlst[i...
9
0
[]
2
smallest-string-with-a-given-numeric-value
Easy code with comments and key idea
easy-code-with-comments-and-key-idea-by-2jxx8
\n// Key idea is to have as many "a"s at the beginning as possible, so that the \n// string will be lexicographically smallest.\n// Similarly, maximize "z"s at
interviewrecipes
NORMAL
2020-11-22T04:02:25.869336+00:00
2020-11-22T04:02:55.717266+00:00
712
false
```\n// Key idea is to have as many "a"s at the beginning as possible, so that the \n// string will be lexicographically smallest.\n// Similarly, maximize "z"s at the end so that the character will be between \n// "a"s and "z"s will be as small as possible.\n// Say, n = 6 and k is 57, then we can\'t have more than 3 "a...
9
2
[]
2
smallest-string-with-a-given-numeric-value
Simple java solution with explaination| o(n) time,o(n) space
simple-java-solution-with-explaination-o-mcy2
we want String of length n and lexographic shortest string as possible.\n2. so take an character array of size n and initialize every index value with \'a\'. an
kushguptacse
NORMAL
2021-01-29T06:33:04.067322+00:00
2022-03-22T06:51:55.405084+00:00
613
false
1. we want String of length n and lexographic shortest string as possible.\n2. so take an character array of size n and initialize every index value with \'a\'. and decrement k.\n3. by doing above step if k reaches 0 it means it is the desired answer. why? becuase all a together are enough to make k sum. \n4. run repea...
8
1
['Greedy', 'Java']
1
smallest-string-with-a-given-numeric-value
simple java solution
simple-java-solution-by-manishkumarsah-3rq7
\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] res = new char[n];\n Arrays.fill(res,\'a\'); // filling the whole
manishkumarsah
NORMAL
2021-01-28T08:43:57.422720+00:00
2021-01-28T08:44:47.349626+00:00
335
false
```\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] res = new char[n];\n Arrays.fill(res,\'a\'); // filling the whole array with starting alphabet a\n \n // now update the k as we have fill the array with character a\n k = k-n;\n \n while...
7
0
[]
0
smallest-string-with-a-given-numeric-value
C++ O(N) Time Greedy
c-on-time-greedy-by-lzl124631x-0874
See my latest update in repo LeetCode\n\n## Solution 1. Greedy\n\nIntuition: We can do it greedily:\n\n If picking a won\'t result in unsolvable problem, we pre
lzl124631x
NORMAL
2020-11-22T04:03:04.578325+00:00
2020-11-22T07:00:15.137565+00:00
602
false
See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1. Greedy\n\n**Intuition**: We can do it greedily:\n\n* If picking `a` won\'t result in unsolvable problem, we prepend `a` to the start of the string.\n* Otherwise, if picking `z` won\'t result in unsolvable problem, we appen...
7
1
[]
0
smallest-string-with-a-given-numeric-value
java || Simple solution || O(n)
java-simple-solution-on-by-funingteng-cca1
Assume we have n length of array with all \'a\', e.g. [\'a\', \'a\', \'a\'] for n = 3, k = 27. The total remaing is 27 - 3 = 24 now. Now we just need to allocat
funingteng
NORMAL
2022-03-24T18:45:25.700945+00:00
2022-03-28T19:44:01.140198+00:00
419
false
Assume we have n length of array with all \'a\', e.g. [\'a\', \'a\', \'a\'] for n = 3, k = 27. The total remaing is 27 - 3 = 24 now. Now we just need to allocate the remaining numbers from right to left. Then iterate the array in reverse order, if larger than 25 (26 - 1), we should allocate \'z\', otherwise we should ...
6
0
['Java']
0
smallest-string-with-a-given-numeric-value
Java Code
java-code-by-rizon__kumar-iezr
\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] c = new char[n];\n \n for(int i = n-1; i>=0; i--){\n
rizon__kumar
NORMAL
2022-03-22T02:50:56.101607+00:00
2022-03-22T02:50:56.101638+00:00
174
false
```\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] c = new char[n];\n \n for(int i = n-1; i>=0; i--){\n int val = Math.min(26,k - i);\n c[i] = (char)(\'a\'+val-1); //using (char) to convert ASCII to respective character\n k=k-val;\n ...
6
0
['Greedy']
1
smallest-string-with-a-given-numeric-value
[Java] Intuitive solution.
java-intuitive-solution-by-sadriddin17-dkjj
The idea is to initialize char array with all value with \'a\' then to reach k we\'ll increase array elements to max from the end to the beginning.\n\n\t\tchar
sadriddin17
NORMAL
2022-03-22T01:42:14.977519+00:00
2022-03-22T01:43:29.058685+00:00
756
false
The idea is to initialize char array with all value with \'a\' then to reach k we\'ll increase array elements to max from the end to the beginning.\n```\n\t\tchar[] result = new char[n];\n for (int i = 0; i < n; i++) {\n result[i] = \'a\';//initialize all array element with \'a\'\n k--;\n ...
6
0
['Java']
2
smallest-string-with-a-given-numeric-value
Correct simple Python solution without loop
correct-simple-python-solution-without-l-fjgg
\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n q, r = divmod(k - n, 25)\n return (\'a\'*(n - q - 1) + chr(ord(\'a\')
dpustovarov
NORMAL
2020-11-23T15:58:22.025745+00:00
2020-11-23T16:23:25.074807+00:00
441
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n q, r = divmod(k - n, 25)\n return (\'a\'*(n - q - 1) + chr(ord(\'a\') + r) if q < n else \'\') + \'z\'*q\n```\n\n* the smallest string is a string like aaa...aszzz...z\n-- a - smallest char (0 to n length)\n-- s - char on the ...
6
0
['Math', 'Python']
0
smallest-string-with-a-given-numeric-value
c++ || very simple solution || O(N)
c-very-simple-solution-on-by-shubhangnau-v3qy
PLEASE UPVOTE IF IT HELPS YOU\n\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n { \n string arr = ""; \n \n for(int i =
shubhangnautiyal
NORMAL
2022-03-22T04:57:21.304686+00:00
2022-03-22T04:57:21.304726+00:00
420
false
**PLEASE UPVOTE IF IT HELPS YOU**\n```\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n { \n string arr = ""; \n \n for(int i = 0; i < n; i++) \n arr += \'a\'; \n for (int i = n - 1; i >= 0; i--) \n { \n k -= i; \n if (k >= 0) \n { \n ...
5
0
['String', 'C']
1
smallest-string-with-a-given-numeric-value
✔️ C++ concise solution | Explanation | minimum time and space complexity
c-concise-solution-explanation-minimum-t-gcd5
Intuition:\nAs we have to create a lexicographically smallest string with a length of n. We have to place as many as the largest number back of the output strin
foysol_ahmed
NORMAL
2022-03-22T04:47:06.673943+00:00
2022-03-22T16:26:36.190312+00:00
252
false
**Intuition:**\nAs we have to create ***a lexicographically smallest string with a length of n***. We have to place as many as the largest number back of the output string. To be more precise, for every iteration, our target is to place ***(i-1) a*** at the beginning, and for the last index, we just put the minimum num...
5
0
['Greedy']
1
smallest-string-with-a-given-numeric-value
Python. 3 lines. faster than 100.00%. cool & clear solution.
python-3-lines-faster-than-10000-cool-cl-kjzn
\tclass Solution:\n\t\tdef getSmallestString(self, n: int, k: int) -> str:\n\t\t\tz = (k - n) // 25\n\t\t\tunique = chr(k - z * 25 - n + 97) if n - z else ""\n\
m-d-f
NORMAL
2021-01-28T12:14:12.458078+00:00
2021-01-28T12:15:25.030201+00:00
472
false
\tclass Solution:\n\t\tdef getSmallestString(self, n: int, k: int) -> str:\n\t\t\tz = (k - n) // 25\n\t\t\tunique = chr(k - z * 25 - n + 97) if n - z else ""\n\t\t\treturn "a"*(n-z-1) + unique + "z"*z
5
1
['Python', 'Python3']
0
smallest-string-with-a-given-numeric-value
Python Math
python-math-by-twerpapple-0vhk
\n"""\nfind the greatest x such that \n 1*x + 26*(n-x) >= k\n 26n - 25x >= k\n 26n - k >= 25x\n (26n-k)/25 >= x\n \n x = floor((26n-k)/25)\n
twerpapple
NORMAL
2020-11-29T17:38:53.097236+00:00
2020-11-29T17:39:28.743732+00:00
136
false
```\n"""\nfind the greatest x such that \n 1*x + 26*(n-x) >= k\n 26n - 25x >= k\n 26n - k >= 25x\n (26n-k)/25 >= x\n \n x = floor((26n-k)/25)\n \n numeric_val = 1*x + 26*(n-x)\n string = [1]*x + [26-(numeric_val-k)] + [26]*(n-x-1)\n"""\n\n\nclass Solution:\n def getSmallestString(self, n, ...
5
0
[]
0
smallest-string-with-a-given-numeric-value
Simple Basic C++ Soln. Only Loop
simple-basic-c-soln-only-loop-by-vardaan-pd93
Intuition\nWe need the smallest string therefore I followed these simple steps to get my ans\n1--> add \'a\' till possible \n2--> add a char \n3--> now add \'z\
vardaanpahwa02
NORMAL
2023-10-27T13:00:18.711045+00:00
2023-10-27T13:00:18.711070+00:00
342
false
# Intuition\nWe need the smallest string therefore I followed these simple steps to get my ans\n1--> add \'a\' till possible \n2--> add a char \n3--> now add \'z\'\n\n# Approach\nNow the question arrises add \'a\' but till when . \nwe will use a number for example let n=3 and k=30.\n1--> add \'a\' then n=2 and k=29\n2-...
4
0
['C++']
2
smallest-string-with-a-given-numeric-value
Javascript | Recursive
javascript-recursive-by-vihangpatel-h9ty
Here we try to find out how many \'z\'s can be placed at the end of the desired output \n Let\'s say n = 3, k = 3; n <= k <= 78 -> All three places are required
vihangpatel
NORMAL
2022-03-23T05:44:29.775750+00:00
2022-03-23T05:44:29.775778+00:00
178
false
Here we try to find out how many \'z\'s can be placed at the end of the desired output \n Let\'s say n = 3, k = 3; n <= k <= 78 -> All three places are required to be filled\n\n 3 -> aaa\n 4 -> aab\n .\n .\n 27 -> aay\n 28 -> aaz\n 29 -> abz\n 30 -> acz\n \nSo, with n = 3, k = 3, lexicographi...
4
0
['JavaScript']
0
smallest-string-with-a-given-numeric-value
Easy Concise C++ O(n) Solution(Faster than 99%)
easy-concise-c-on-solutionfaster-than-99-ooht
class Solution {\npublic:\n string getSmallestString(int n, int k) {\n int curr=1;\n string ans=""; \n for(int i=1;i<=n;i++){\n if(
kartiksaxenaabcd
NORMAL
2022-03-22T11:30:28.243614+00:00
2022-03-22T11:34:12.129254+00:00
142
false
class Solution {\npublic:\n string getSmallestString(int n, int k) {\n int curr=1;\n string ans=""; \n for(int i=1;i<=n;i++){\n if((k-curr)< 26*(n-i)){\n ans+=char(\'a\'+curr-1);\n k-=curr;\n }\n else{\n while((k-curr)>26*(n-i))\n ...
4
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
Backward-filling with an early exit [bad code!]
backward-filling-with-an-early-exit-bad-s1iya
There is one optimization in the code below that is helpful for large n: As soon as k == n, we know that the rest of the sought string is \'a\' (s. && k > n in
anikit
NORMAL
2022-03-22T10:24:23.935345+00:00
2023-01-03T23:08:09.311835+00:00
101
false
There is one optimization in the code below that is helpful for large `n`: As soon as `k == n`, we know that the rest of the sought string is `\'a\'` (s. `&& k > n` in the `while` condition).\n\nWhile this code works, there are a few bad coding practices here which should be avoided:\n1. Re-use of variables: the argume...
4
0
['C#']
0
smallest-string-with-a-given-numeric-value
C++ two approaches || commented
c-two-approaches-commented-by-suryapsg-wxh4
Approach 1:\n\nstring getSmallestString(int n, int k) {\n\tstring res;\n\tint c;\n\tfor(int i=0;i<n;i++){\n\t\t//start with \'a\'\n\t\tc=1;\n\t\t//If current in
SuryaPSG
NORMAL
2022-03-22T03:06:46.622178+00:00
2022-03-22T04:16:55.727661+00:00
305
false
**Approach 1:**\n```\nstring getSmallestString(int n, int k) {\n\tstring res;\n\tint c;\n\tfor(int i=0;i<n;i++){\n\t\t//start with \'a\'\n\t\tc=1;\n\t\t//If current index holds \'a\' then remaining n-i-1 length should be enough to hold atleast \'z\' in every index\n\t\t//if it is not enough we make the current char b,c...
4
0
['C']
1
smallest-string-with-a-given-numeric-value
[Simple and readable] - O(N) TypeScript/JavaScript
simple-and-readable-on-typescriptjavascr-aq9w
\nfunction getSmallestString(n: number, k: number): string {\n const alphabet = [\'\',\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\'
dustinkiselbach
NORMAL
2021-01-28T15:33:47.613795+00:00
2021-01-28T15:33:47.613836+00:00
275
false
```\nfunction getSmallestString(n: number, k: number): string {\n const alphabet = [\'\',\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\']\n let sum = 0\n let s = \'\'\n \n for (let i = n - 1; i >= ...
4
0
['TypeScript', 'JavaScript']
1
smallest-string-with-a-given-numeric-value
Smallest String With A Given Numeric Value: Python, 3 LoC
smallest-string-with-a-given-numeric-val-3qmq
python\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n if k == 26*n: return \'z\' * n\n nz, nshift = divmod(k - n, 25)
sjoin
NORMAL
2021-01-28T15:18:45.019804+00:00
2021-01-28T15:18:45.019838+00:00
185
false
```python\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n if k == 26*n: return \'z\' * n\n nz, nshift = divmod(k - n, 25)\n return \'a\' * (n - nz - 1) + chr(ord(\'a\') + nshift) + \'z\' * nz\n```\n\n---\nIf you find this helpful, please **upvote**! Thank you! :-)
4
0
['Math', 'Python']
0
smallest-string-with-a-given-numeric-value
O(n) Better Solution Greedy [Cpp]
on-better-solution-greedy-cpp-by-anil111-54jb
\nclass Solution {\npublic:\n string getSmallestString(int n, int k) \n {\n string str(n,\'a\');\n k=k-n;\n int ri=n-1;\n whil
anil111
NORMAL
2021-01-28T08:55:35.295039+00:00
2021-01-28T09:05:27.751520+00:00
276
false
```\nclass Solution {\npublic:\n string getSmallestString(int n, int k) \n {\n string str(n,\'a\');\n k=k-n;\n int ri=n-1;\n while(k>0)\n {\n str[ri]=str[ri]+min(25,k);\n ri--;\n k=k-25;\n }\n return str;\n }\n};\n```
4
0
['Greedy', 'C++']
0
smallest-string-with-a-given-numeric-value
Python - !beautiful && !concise && !short
python-beautiful-concise-short-by-kafola-h41x
Start with all "a" then from the end bump it up as much as posible (25 max to turn "a" into "z")\n\n\ndef getSmallestString(self, n: int, k: int) -> str:\n\t\ta
kafola
NORMAL
2020-11-22T04:04:18.287214+00:00
2020-11-22T04:04:18.287246+00:00
313
false
Start with all "a" then from the end bump it up as much as posible (25 max to turn "a" into "z")\n\n```\ndef getSmallestString(self, n: int, k: int) -> str:\n\t\tarr = [97] * n\n k -= n\n \n i = n - 1\n while k > 0 and i > -1:\n arr[i] += min(k, 25)\n k -= min(k, 25)\n ...
4
0
[]
2
smallest-string-with-a-given-numeric-value
C++ Easy Code || Fully Explained in Simple Steps || Clean and Clear code + Explanation
c-easy-code-fully-explained-in-simple-st-ej36
\nThe most clear explanation of the algorithm is as:\n\nStep - 1 Initialize a string s with n number of \'a\' to make it as a smallest possible lexiographic.\n
dee_stroyer
NORMAL
2022-03-23T19:34:14.637065+00:00
2022-03-23T19:34:35.067496+00:00
140
false
\n**The most clear explanation of the algorithm is as:**\n\n*Step - 1 Initialize a string s with n number of \'a\' to make it as a smallest possible lexiographic.\nStep - 2 Count of \'a = 1\' , so if i have n number of \'a\' it means it sum is also \'n\'.\nStep - 3 Reduce the value of k by n (i.e k=k-n);\nStep - 4 Sta...
3
0
['String', 'Greedy', 'C', 'C++']
0
smallest-string-with-a-given-numeric-value
1663 | C++ | Easy O(N) solution with explanation
1663-c-easy-on-solution-with-explanation-9s7f
Please upvote if you like this solution :)\n\nApproach:\n For getting smallest lexicographical order of string, Initially we define n size string and put \'a\'s
Yash2arma
NORMAL
2022-03-22T18:33:21.921805+00:00
2022-03-22T18:33:21.921875+00:00
72
false
**Please upvote if you like this solution :)**\n\n**Approach:**\n* For getting smallest lexicographical order of string, Initially we define n size string and put \'a\'s into it.\n* Since string only contains \'a\'s we subtract n from k i.e. k=k-n;\n* Now, we traverse from the end of the string because we want smallest...
3
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
Simple java solution
simple-java-solution-by-siddhant_1602-4rwo
class Solution {\n\n public String getSmallestString(int n, int k) {\n char c[]=new char[n];\n Arrays.fill(c,\'a\');\n int i=n-1;\n
Siddhant_1602
NORMAL
2022-03-22T17:49:02.895517+00:00
2022-03-22T17:49:02.895562+00:00
203
false
class Solution {\n\n public String getSmallestString(int n, int k) {\n char c[]=new char[n];\n Arrays.fill(c,\'a\');\n int i=n-1;\n k-=n;\n while(k>0)\n {\n if(k>25)\n {\n c[i--]=\'z\';\n k-=25;\n }\n ...
3
0
['Java']
0
smallest-string-with-a-given-numeric-value
O(n) C++ solution with explanation and dry run
on-c-solution-with-explanation-and-dry-r-g7gd
We have to find the smallest lexiographical string with size of n and numeric value of k.\n\nFor a string to be lexiographically small, it\'s beginning charact
shivansh961
NORMAL
2022-03-22T13:23:20.176319+00:00
2022-03-22T13:52:57.518385+00:00
135
false
We have to find the smallest lexiographical string with size of n and numeric value of k.\n\n**For a string to be lexiographically small, it\'s beginning characters must be smallest.**\n\nWe initialise a string as **ans** with size n, having each character as **\'a\'**. \n\nLet\'s take an example for the better unders...
3
0
[]
2
smallest-string-with-a-given-numeric-value
✅ Python Solution
python-solution-by-dhananjay79-qk56
\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n ans = \'\'\n while (n - 1) * 26 >= k:\n ans += \'a\'\n
dhananjay79
NORMAL
2022-03-22T12:40:26.217732+00:00
2022-03-22T12:40:26.217764+00:00
309
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n ans = \'\'\n while (n - 1) * 26 >= k:\n ans += \'a\'\n n -= 1; k -= 1\n ans += chr(ord(\'a\') + (k % 26 or 26) - 1)\n ans += \'z\' * (n - 1)\n return ans\n```
3
0
['Python', 'Python3']
0
smallest-string-with-a-given-numeric-value
Simple Java Code
simple-java-code-by-sai_prashant-z8ia
Idea is simple, we will make the string from end greedily i.e, putting the lexographically biggest possible character. Here is my code \n\n\nclass Solution {\n
sai_prashant
NORMAL
2022-03-22T06:04:36.355636+00:00
2022-03-22T06:04:36.355687+00:00
169
false
Idea is simple, we will make the string from end greedily i.e, putting the lexographically biggest possible character. Here is my code \n\n```\nclass Solution {\n public String getSmallestString(int n, int k) {\n StringBuilder sb=new StringBuilder("");\n for(int i=0;i<n;i++){\n int val=k-(n-...
3
0
['Greedy']
0
smallest-string-with-a-given-numeric-value
1. Failed DP, 2. Greedy
1-failed-dp-2-greedy-by-akshay3213-5qsb
\n\n\n# Using dynamic programming - Gave me memory limit exceeding\n# \n\n\nclass Solution {\n\t//O(n * k)\n public String getSmallestString(int n, int k) {\
akshay3213
NORMAL
2022-03-22T03:53:58.020877+00:00
2022-03-22T03:59:52.624913+00:00
144
false
![image](https://assets.leetcode.com/users/images/4bcaacfb-8182-492c-a7ac-b30318949413_1647920423.2616134.png)\n\n\n# Using dynamic programming - Gave me memory limit exceeding\n# ![image](https://assets.leetcode.com/users/images/5efebbec-384a-4429-ad58-417261dbf7ff_1647920476.4489262.png)\n\n```\nclass Solution {\n\t/...
3
0
['Dynamic Programming', 'Greedy']
0
smallest-string-with-a-given-numeric-value
[Python3] O(n) time and O(1) space complexity solution
python3-on-time-and-o1-space-complexity-cuqu1
python3\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n result =[0]*n\n for position in range(n-1,-1,-1):\n
bhushandhumal
NORMAL
2022-03-22T03:20:49.899761+00:00
2022-03-22T04:12:13.413894+00:00
360
false
``` python3\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n result =[0]*n\n for position in range(n-1,-1,-1):\n add = min(k -position,26)\n result[position] = chr(ord("a")+add -1)\n k-=add\n \n return "".join(result)\n```\n\nCo...
3
0
['Python', 'Python3']
1
smallest-string-with-a-given-numeric-value
python 3 || O(n)
python-3-on-by-derek-y-3xzb
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n res = \'\'\n for i in range(n):\n q, r = divmod(k, 26)\
derek-y
NORMAL
2022-03-22T01:36:15.876719+00:00
2022-03-24T03:38:39.913835+00:00
194
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n res = \'\'\n for i in range(n):\n q, r = divmod(k, 26)\n if r == 0:\n q -= 1\n r = 26\n\n if n - q - i >= 2:\n res += \'a\'\n k -= 1\...
3
0
['Greedy', 'Python', 'Python3']
0
smallest-string-with-a-given-numeric-value
C++ || Easy Explanation || With Comments || TC - O(N)
c-easy-explanation-with-comments-tc-on-b-hml3
\n\n1. Create a string of size n with all \'a\'s in it\n2. Start from end of the created string \n3. Create a variable \'remaining\' and put \'z\' at index i if
krishna__1902
NORMAL
2022-03-22T01:12:48.084374+00:00
2022-03-22T01:16:06.898575+00:00
59
false
\n\n**1. Create a string of size n with all \'a\'s in it\n2. Start from end of the created string \n3. Create a variable \'remaining\' and put \'z\' at index i if remaining>25 (i.e, \'a\'+25 , as a is already there so \'a\'+25 = \'z\'\n4. If remaining becomes less than or equal to 25, put \'a\' + remaining value at tha...
3
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
[Python] O(1) with explanation
python-o1-with-explanation-by-shoomoon-dudy
Accoding to the defination, the smallest lexicographically string must have the patten as \n\n\t\ts = \'a\' * p + t * q + \'z\' * r\n\t\twhere p, r >= 0, \'a\'
shoomoon
NORMAL
2021-02-25T19:59:43.811115+00:00
2021-02-25T21:45:42.951225+00:00
139
false
Accoding to the defination, the smallest lexicographically string must have the patten as \n\n\t\ts = \'a\' * p + t * q + \'z\' * r\n\t\twhere p, r >= 0, \'a\' < t < \'z\', q = 0 or 1\n\nThus, we have:\n```\n1. k = 1 * p + val_t * q + 26 * r\n2. p + q + r = n\n```\n\nThen k = (n - q - r) + val_t * q + 26 * r = n + q * ...
3
0
[]
0
smallest-string-with-a-given-numeric-value
C# - O(N) - Fill array from end
c-on-fill-array-from-end-by-christris-wob7
csharp\npublic string GetSmallestString(int n, int rem)\n{\n\t char[] result = new char[n];\n\t Array.Fill(result, \'a\');\n\n\t // We have already filled array
christris
NORMAL
2021-01-29T02:15:54.808001+00:00
2021-01-29T02:17:22.806458+00:00
95
false
```csharp\npublic string GetSmallestString(int n, int rem)\n{\n\t char[] result = new char[n];\n\t Array.Fill(result, \'a\');\n\n\t // We have already filled array with \'a\' with value 1 for each char\n\t rem -= n;\n\n\t for(int i = n - 1; i >= 0; i--)\n\t {\n\t\t int diff = Math.Min(rem, 25);\n\t\t result[i] = (char)...
3
0
[]
0
smallest-string-with-a-given-numeric-value
[C++] Greedy Approach
c-greedy-approach-by-codedayday-p178
Approach 1: Greedy Solution\nIdea:\nGenerate the initial string with all \'a\' characters. This will reduce k by n.\nThen, turn rightmost \'a\' into \'z\' (\'a\
codedayday
NORMAL
2021-01-28T15:12:32.183431+00:00
2021-01-28T15:12:32.183471+00:00
141
false
Approach 1: Greedy Solution\nIdea:\nGenerate the initial string with all \'a\' characters. This will reduce k by n.\nThen, turn rightmost \'a\' into \'z\' (\'a\' + 25, or \'a\' + k) while k is positive.\n```\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n string ans(n, \'a\');\n ...
3
0
[]
1
smallest-string-with-a-given-numeric-value
[C++] Single-pass Solution Explained, ~100% Time, ~98% Space
c-single-pass-solution-explained-100-tim-iumc
Alright: while this problem might tempt you to actually compute all/some permutations, the size of the expected string is such that it should definitely make us
ajna
NORMAL
2021-01-28T14:15:36.723067+00:00
2021-01-28T14:15:36.723093+00:00
201
false
Alright: while this problem might tempt you to actually compute all/some permutations, the size of the expected string is such that it should definitely make us very wary about how to proceed in terms of performance.\n\nBut we can go through an actually smarter way, once we figure out that you average result will have ...
3
0
['C', 'Combinatorics', 'Probability and Statistics', 'C++']
0
smallest-string-with-a-given-numeric-value
Rust 4ms solution
rust-4ms-solution-by-sugyan-xdfc
\nimpl Solution {\n pub fn get_smallest_string(n: i32, k: i32) -> String {\n let mut v = vec![0; n as usize];\n let mut k = k - n;\n for
sugyan
NORMAL
2021-01-28T08:53:01.060448+00:00
2021-01-28T08:53:01.060498+00:00
78
false
```\nimpl Solution {\n pub fn get_smallest_string(n: i32, k: i32) -> String {\n let mut v = vec![0; n as usize];\n let mut k = k - n;\n for e in v.iter_mut().rev() {\n let m = std::cmp::min(25, k);\n *e = m as u8 + b\'a\';\n k -= m;\n }\n String::fr...
3
0
['Rust']
1
smallest-string-with-a-given-numeric-value
[Java] O(logN) Solution, Divide And Conquer, 3ms Faster than 100%
java-ologn-solution-divide-and-conquer-3-3f95
Use divide and conquer when adding \'a\' and \'z\'.\n\nclass Solution {\n public String getSmallestString(int n, int k) {\n int subK = k - n; \n
applechen
NORMAL
2020-11-22T06:44:16.119975+00:00
2020-11-22T06:44:16.120009+00:00
326
false
Use divide and conquer when adding \'a\' and \'z\'.\n```\nclass Solution {\n public String getSmallestString(int n, int k) {\n int subK = k - n; \n int numZ = subK / 25;\n char mid = (char)(\'a\' + subK % 25);\n int numA = n - numZ - 1;\n \n StringBuilder res = new StringBui...
3
1
['Divide and Conquer', 'Java']
4
smallest-string-with-a-given-numeric-value
Python 3 | 5-line Greedy O(N) | Explanation
python-3-5-line-greedy-on-explanation-by-n6we
Understand the question\n- Question is asking for smallest string, that being said, \n\t- we want to have as most a on the left side as possible\n\t- If using a
idontknoooo
NORMAL
2020-11-22T05:29:53.794647+00:00
2020-11-22T07:27:44.812235+00:00
227
false
### Understand the question\n- Question is asking for *smallest string*, that being said, \n\t- we want to have as most *a* on the left side as possible\n\t- If using *a* doesn\'t match given number, we can try some letter with larger number \n\t- *z* will be the last letter we want to use, so we want to have them all ...
3
0
['Greedy', 'Python', 'Python3']
2
smallest-string-with-a-given-numeric-value
[C++] Simple O(N) Solution | Brute Force Solution | 2 Solutions
c-simple-on-solution-brute-force-solutio-fv09
\nclass Solution\n{\npublic:\n // Best Solution\n string getSmallestString(int n, int k)\n {\n string res(n, \'a\');\n k -= n;\n i
ravireddy07
NORMAL
2020-11-22T04:51:07.569238+00:00
2020-11-22T04:52:08.277399+00:00
58
false
```\nclass Solution\n{\npublic:\n // Best Solution\n string getSmallestString(int n, int k)\n {\n string res(n, \'a\');\n k -= n;\n int midChar = 0;\n while (k > 0)\n {\n midChar = min(k, 25);\n k -= midChar;\n res[n - 1] += midChar;\n ...
3
1
[]
0
smallest-string-with-a-given-numeric-value
Python simple O(N) math solution with comments
python-simple-on-math-solution-with-comm-wiwp
k = 73, n = 5\n526 -73 = 57\n\nz z z z z\n26 26 26 26 26\n| | | | |\n25 + 25 + 7 + 0 + 0
SonicLLM
NORMAL
2020-11-22T04:48:35.332032+00:00
2020-11-22T21:33:10.247673+00:00
286
false
k = 73, n = 5\n5*26 -73 = 57\n```\nz z z z z\n26 26 26 26 26\n| | | | |\n25 + 25 + 7 + 0 + 0 = 57\n| | | | |\n1 1 19 0 0 \na a s z z\n```\n\n57 = 2 * 25 + 7 * 1\ndiv = 2, remain = 1\n=...
3
0
['Math', 'Python']
3
smallest-string-with-a-given-numeric-value
[c++] easy with example
c-easy-with-example-by-sanjeev1709912-1f8s
My idea behind this code to take character gridily \nmeans first i try to pick 26 if possible \n\nNow the question is how to check if possible \nSo here is answ
sanjeev1709912
NORMAL
2020-11-22T04:32:17.625153+00:00
2020-11-22T05:08:24.565124+00:00
122
false
My idea behind this code to take character gridily \nmeans first i try to pick 26 if possible \n\nNow the question is how to check if possible \nSo here is answer like if we pick 26 and the remaining value is geater then required number of element then it safe to pick 26 thats it\n\nok now see example \n```\nlet think ...
3
0
[]
0
smallest-string-with-a-given-numeric-value
[Ruby] 3 lines. Intuitive + math solution with system of linear equations
ruby-3-lines-intuitive-math-solution-wit-pv0t
Algorithm\n\nSearch an answer in a form aa...aa[LETTER]zz...zz\nNumeric Value of that string is a number of a chars * 1 + number of z chars * 26 + value of LETT
shhavel
NORMAL
2020-11-22T04:17:54.246832+00:00
2021-01-28T08:51:33.962698+00:00
203
false
# Algorithm\n\nSearch an answer in a form `aa...aa[LETTER]zz...zz`\nNumeric Value of that string is a number of `a` chars * 1 + number of `z` chars * 26 + value of `LETTER`.\n\nMin value of a string of len `n` is `n` for string `aa...aaa`.\nAfter decreasing needed sum (value of string result) by n count of `z` is `s /...
3
0
['Math', 'Ruby']
0
smallest-string-with-a-given-numeric-value
BEATS 99% || EASY with Eg|| In Depth Commented CODE.
beats-99-easy-with-eg-in-depth-commented-5oc4
This provided code defines a function getSmallestString(n, k) that generates a lexicographically smallest string with the following properties:\n\n- Length: The
Abhishekkant135
NORMAL
2024-06-26T10:22:19.698413+00:00
2024-06-26T10:22:19.698440+00:00
218
false
This provided code defines a function `getSmallestString(n, k)` that generates a lexicographically smallest string with the following properties:\n\n- **Length:** The string has a length of `n` characters.\n- **Maximum Zs:** The string can contain at most `k` characters \'z\'.\n\n**Functionality Breakdown:**\n\n1. **Ch...
2
0
['String', 'Greedy', 'Java']
0
smallest-string-with-a-given-numeric-value
c++ | easy | fast
c-easy-fast-by-venomhighs7-stso
\n\n# Code\n\n//from hi-malik\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n string res(n,\'a\');\n k -= n;\n \
venomhighs7
NORMAL
2022-11-22T12:17:02.074263+00:00
2022-11-22T12:17:02.074298+00:00
224
false
\n\n# Code\n```\n//from hi-malik\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n string res(n,\'a\');\n k -= n;\n \n while(k > 0){\n res[--n] += min(25, k);\n k -= min(25, k);\n }\n return res;\n }\n};\n```
2
0
['C++']
1
smallest-string-with-a-given-numeric-value
Easy to understand with comments
easy-to-understand-with-comments-by-shas-76en
we start from the back , keep adding the highest letter possible to the result , highest possible letter is k-(remaining spaces)(remaining as we subtract the va
shashank_c10
NORMAL
2022-03-23T17:47:24.950988+00:00
2022-03-23T17:47:24.951048+00:00
200
false
we start from the back , keep adding the highest letter possible to the result , highest possible letter is k-(remaining spaces)(remaining as we subtract the value of k by the value of each letter added, the remaining spaces are n - current length of result)\n```\ndef getSmallestString(self, n: int, k: int) -> str:\n ...
2
0
['Greedy', 'Python', 'Python3']
0
smallest-string-with-a-given-numeric-value
✅C++ || TC - O(N), SC- O(1) || 🗓️ Daily LeetCoding Challenge || March || Day 22
c-tc-on-sc-o1-daily-leetcoding-challenge-0sja
Approach: \n1) Since we want to make a string lexicographically smaller so we will make a string of size n with \'a\'.\n2) Start from the right to left characte
shm_47
NORMAL
2022-03-22T18:08:49.871209+00:00
2022-03-22T18:19:37.371832+00:00
38
false
**Approach:** \n1) Since we want to make a string lexicographically smaller so we will make a string of size n with \'a\'.\n2) Start from the right to left character, increase the char value by min(25, k) until k will reach to 0.\n\n```\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n s...
2
0
[]
0
smallest-string-with-a-given-numeric-value
✅ C++ || Dry Run || Easy || 🗓️ Daily LeetCoding Challenge || March || Day 22
c-dry-run-easy-daily-leetcoding-challeng-scvt
Please Upvote If It Helps\n\nAlgorithm\n Initialise an string of length n with all \'a\'s\n\n Start traversing from the end of the string while k is positive\n
mayanksamadhiya12345
NORMAL
2022-03-22T17:12:17.052153+00:00
2022-03-22T17:12:17.052200+00:00
34
false
**Please Upvote If It Helps**\n\n**Algorithm**\n* **Initialise** an string of length **n** with all **\'a\'s**\n\n* **Start traversing** from the **end of the string** while **k is positive**\n* add the element of the string by **\'z\' if k >= 25**\n* else add the **ascii value character** \n* At the same time decrease...
2
0
[]
1
smallest-string-with-a-given-numeric-value
Python | Reverse filling | Easy and clean solution
python-reverse-filling-easy-and-clean-so-j5oz
\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n result, k, i = [\'a\']*n, k-n, n-1\n while k:\n
revanthnamburu
NORMAL
2022-03-22T13:46:42.035521+00:00
2022-03-22T13:46:42.035565+00:00
143
false
```\nclass Solution:\n def getSmallestString(self, n: int, k: int) -> str:\n result, k, i = [\'a\']*n, k-n, n-1\n while k:\n k = k + 1\n if k/26 >= 1:\n result[i], k, i = \'z\', k-26, i-1\n else:\n result[i], k =...
2
0
['String', 'Python']
0
smallest-string-with-a-given-numeric-value
✅ C++ Solution with explanation | O(n)
c-solution-with-explanation-on-by-karyga-45yw
Explanation\n\n1. Initialise a string ans all with \'a\'s (to ensure that the answer will be lexicographically small\n2. Start traversing the string from the en
karygauss03
NORMAL
2022-03-22T10:25:49.631977+00:00
2022-03-22T10:25:49.632001+00:00
32
false
**Explanation**\n```\n1. Initialise a string ans all with \'a\'s (to ensure that the answer will be lexicographically small\n2. Start traversing the string from the end, and if k >= 25 we add \'z\' instead of \'a\' else we add \n\'b\' or \'c\' or .... \'y\' depending on k. (ans[i] += min(25, k)).\n3. We decrease the va...
2
0
[]
0
smallest-string-with-a-given-numeric-value
C++|| easy to understand || with comments || greedy
c-easy-to-understand-with-comments-greed-kce5
\nstring getSmallestString(int n, int k) {\n string ans="";\n for(int i=0;i<n;i++)\n {\n //for getting the lexicographically smalles
priyanshu12k5
NORMAL
2022-03-22T09:46:02.014049+00:00
2022-03-22T09:46:02.014082+00:00
71
false
```\nstring getSmallestString(int n, int k) {\n string ans="";\n for(int i=0;i<n;i++)\n {\n //for getting the lexicographically smallest string\n ans+=\'a\';\n }\n k=k-n;\n int j=n-1;\n while(k!=0 && j>=0)\n {\n// as we have added a no...
2
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
✔️[C++] || 6 line Simple Code || Easy to understand || TC: O( n ) , SC: O( 1 )
c-6-line-simple-code-easy-to-understand-8025t
```\nstring getSmallestString(int n, int k) {\n string res(n,\'a\');\n for(int i=0;i=26) res[n-1-i]=\'z\' , k-=26;\n else res[n-1-i]=c
anant_0059
NORMAL
2022-03-22T09:34:45.675105+00:00
2022-03-22T09:34:45.675151+00:00
289
false
```\nstring getSmallestString(int n, int k) {\n string res(n,\'a\');\n for(int i=0;i<n;++i){\n int remain=k-(n-1-i);\n if(remain>=26) res[n-1-i]=\'z\' , k-=26;\n else res[n-1-i]=char(\'a\'+remain-1) , k=n-i-1;\n }\n return res;\n }
2
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
C++ Easy Explanation Optimal Approach
c-easy-explanation-optimal-approach-by-j-w827
Approach\nstart with a string of all a\'s of length n as it is the lexicographically smallest string possible\nupdate k as k-n\n\nnow we traverse our string fro
jay_suk23
NORMAL
2022-03-22T09:27:50.231788+00:00
2022-03-22T09:27:50.231835+00:00
26
false
**Approach**\nstart with a string of all a\'s of length n as it is the lexicographically smallest string possible\nupdate k as k-n\n\nnow we traverse our string from right to left and try to fill it with the maximum character possible (this will make sure that we get the lowest characters to the left)\n\n\n**fillling i...
2
0
['String', 'Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
c++ | Easy approach | Greedy | with explanation | with example
c-easy-approach-greedy-with-explanation-qz9m3
Input: n = 5, k = 73\nOutput: "aaszz"\n\nstep 1: make string of length n=5 and fill it with \'a\'. after step 1 our ans string will look like ans="aaaaa"\nstep
akashjoshi99
NORMAL
2022-03-22T09:15:48.145528+00:00
2022-03-22T09:15:48.145562+00:00
140
false
Input: n = 5, k = 73\nOutput: "aaszz"\n\nstep 1: make string of length n=5 and fill it with \'a\'. after step 1 our **ans** string will look like **ans="aaaaa"**\nstep 2: iterate from back and keep the track of **sumOfString** after replacing ith index with \'z\'. sumOfString(aaaaz) is 1+1+1+1+26=30.\nstep 3: if sumOfS...
2
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
Math with short proof | beats 98.62% / 99.54%
math-with-short-proof-beats-9862-9954-by-efnq
This solution is based on the fact that every output will always be of the form aaa...aaa?zzz...zzz regardless of the values of k and n.\n\nThis submission ran
sr_vrd
NORMAL
2022-03-22T08:09:59.103240+00:00
2022-03-22T12:57:59.394816+00:00
55
false
This solution is based on the fact that every output will always be of the form `aaa...aaa?zzz...zzz` *regardless* of the values of `k` and `n`.\n\nThis submission ran at **32 ms** and used **14.8 MB** of memory. At the time of submission it was **faster than 98.62%** Python3 submissions, and used **less memory than 99...
2
0
['Math']
0
smallest-string-with-a-given-numeric-value
✅ JAVA || 10 LINES || ONE TRAVERSAL || EASY || SIMPLE ||
java-10-lines-one-traversal-easy-simple-q4dhh
JAVAEASY CLEAN & READABLE\n\n\n\n* Our main goal is to build a string lexiographically smallest.\n* So the smallest possible string for any n would be full of \
bharathkalyans
NORMAL
2022-03-22T07:50:01.168608+00:00
2022-03-22T07:50:01.168640+00:00
95
false
``` ```**JAVA**``` ```**EASY**``` ``` ``` ```**CLEAN & READABLE**``` ```\n\n\n```\n* Our main goal is to build a string lexiographically smallest.\n* So the smallest possible string for any n would be full of \'a\'.\n* Now we have to fuflill another condition that is we want the sum of characters to be some k.\n* By ...
2
1
['Java']
1
smallest-string-with-a-given-numeric-value
C++ || Greedy Approch && Reverse filling || O(n) time
c-greedy-approch-reverse-filling-on-time-rtzl
\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n \n string res = "";\n \n while(n>0){\n \n
pandeyji2023
NORMAL
2022-03-22T07:00:26.458645+00:00
2022-03-22T07:00:26.458670+00:00
26
false
```\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n \n string res = "";\n \n while(n>0){\n \n for(int i=26;i>=1;i--){\n \n int rem = k-i;\n if(rem >= n-1){\n char c = i-1+\'a\';\n ...
2
0
[]
0
smallest-string-with-a-given-numeric-value
You will love it || Java || Simple Solution || Well Commented
you-will-love-it-java-simple-solution-we-uzt8
Very Easy Solution. I have commented all steps.\n\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] ch = new char[n]; // In
samyakdarshan97
NORMAL
2022-03-22T06:21:30.311106+00:00
2022-03-22T06:21:30.311147+00:00
175
false
Very Easy Solution. I have commented all steps.\n```\nclass Solution {\n public String getSmallestString(int n, int k) {\n char[] ch = new char[n]; // Initially taking a character array and later we will convert it to String.\n int i=0;\n for(;i<n;i++){\n ch[i] = \'a\'; // Initially k...
2
0
['String', 'Greedy', 'Java']
0
smallest-string-with-a-given-numeric-value
By Math
by-math-by-haiyunjin-ktx7
This is simply a math problem.\n\n1. Fill all with "a".\n2. Then for each extra number, add from the right. The number of "z"\'s on the right can be calculated
haiyunjin
NORMAL
2022-03-22T06:05:28.215800+00:00
2022-03-22T06:05:51.132263+00:00
31
false
This is simply a math problem.\n\n1. Fill all with "a".\n2. Then for each extra number, add from the right. The number of "z"\'s on the right can be calculated as `extra/25`.\n3. The letter right after "a"\'s can also be calculated by `\'a\' + extra%25`\n4. Then just add "a"\'s, first non-\'a\' and fill rest with "z".\...
2
0
['Math', 'Java']
0
smallest-string-with-a-given-numeric-value
✅ Easy-Peasy Solution | Greedy
easy-peasy-solution-greedy-by-igi17-pbaq
Intuition- At each index of string take smallest character possible such that remaining character \n are possible to fill with remaining k value.
igi17
NORMAL
2022-03-22T05:56:39.141779+00:00
2022-03-22T05:59:54.515901+00:00
78
false
***Intuition***- At each index of string take smallest character possible such that remaining character \n are possible to fill with remaining k value.\n\n\tclass Solution {\n\tpublic:\n\t\tstring getSmallestString(int n, int k) {\n\t\t\tstring ans;\n\t\t\tfor(int i=0;i<n;i++){\n\t\t\t\tfor(int j=0;j<26;...
2
0
['Greedy', 'C']
0
smallest-string-with-a-given-numeric-value
C++ | one line solution, fast solution and explanation |O(n) or can O(1)?
c-one-line-solution-fast-solution-and-ex-tfp2
1. One line solution\nThe solution can be combined by three kind of string, so we have the one line solution. \nC++\nclass Solution {\npublic:\n string getSm
milochen
NORMAL
2022-03-22T05:39:13.563841+00:00
2022-03-22T05:39:13.563867+00:00
141
false
# 1. One line solution\nThe solution can be combined by three kind of string, so we have the one line solution. \n```C++\nclass Solution {\npublic:\n string getSmallestString(int n, int k) {\n\t\treturn string(n-(k-n)/25-((k-n)%25!=0), \'a\') + string(((k-n)%25!=0),\'a\'+(k-n)%25) + string((k-n)/25,\'z\');\n\t}\n};\...
2
0
['C']
0
smallest-string-with-a-given-numeric-value
Greedy Python Solution with Explanation
greedy-python-solution-with-explanation-c78xw
Observation:\nFor any test case we will have some number of \'a\' in the start one or zero character from \'b\' to \'y\' in the middle and some number of \'z\'
ancoderr
NORMAL
2022-03-22T04:47:28.337047+00:00
2022-03-22T04:52:46.093766+00:00
195
false
**Observation:**\nFor any test case we will have some number of \'a\' in the start one or zero character from \'b\' to \'y\' in the middle and some number of \'z\' in the end.\n\n**Intuition:**\nWe want the lexicographically smallest string. If we have a choice to either chose b/w **`az`** and **`by`**, we will chose *...
2
0
['Greedy', 'Python', 'Python3']
0