C
C#9mo ago
morry329#

❔ Why is Pop() necessary here??

So I found a sample solution for the LeetCode decode string puzzle https://leetcode.com/problems/decode-string/
public class Solution
{
public string DecodeString(string s)
{
Stack<string> strings = new Stack<string>();
Stack<int> numbers = new Stack<int>();

string currentString = "";
int currentNumber = 0;

foreach (char c in s)
{
if (c == '[')
{
strings.Push(currentString);
numbers.Push(currentNumber);
currentString = "";
currentNumber = 0;
}
else if(c == ']')
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
else if ( c-'0'<=9 && c - '0' >= 0)
currentNumber = currentNumber * 10 + (c - '0');
else
currentString += c;
}

return currentString;
}
}
public class Solution
{
public string DecodeString(string s)
{
Stack<string> strings = new Stack<string>();
Stack<int> numbers = new Stack<int>();

string currentString = "";
int currentNumber = 0;

foreach (char c in s)
{
if (c == '[')
{
strings.Push(currentString);
numbers.Push(currentNumber);
currentString = "";
currentNumber = 0;
}
else if(c == ']')
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
else if ( c-'0'<=9 && c - '0' >= 0)
currentNumber = currentNumber * 10 + (c - '0');
else
currentString += c;
}

return currentString;
}
}
` And I am confused with this line
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
currentString = strings.Pop() + string.Join("", Enumerable.Repeat(currentString, numbers.Pop()));
` Why is it strings.Pop() needed here?? ChatGPT says it's not needed and it's redundant, I do not think that is the correct answer. Does the Pop remove the bracket [, so the code goes on to check part of the string??
LeetCode
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
4 Replies
phaseshift
phaseshift9mo ago
Debug it and watch
Tvde1
Tvde19mo ago
Stack.Pop Method (System.Collections)
Removes and returns the object at the top of the Stack.
Tvde1
Tvde19mo ago
Removes and returns the object at the top of the Stack.
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
I don't really get how to create new methods for my code. Yesterday people told me it's got no body.Didn't really understand anything when i did research. It's probably a quick fix : ] I'm talking ab❔ MC3050 compiler errorI added a new project with some custom WPF controls to my project's dependencies. I then added the ✅ Function For Division Game Showing Same Number Twice???```cs public void StartDivGame(string username, DateTime date, int totalQuestions, string difficultygoto vs recursionHello, I have a little question about "good habit" in this block of codes. So a little context is, t❔ What's the best way to test a URL that's it's valid?I have a situation where the user can enter a URL if they so choose, but if they do, I want to ensur❔ How to Package Multiple Versions of a Library Targeting Different Dependency Versions into a NugetI am working on a C# library (`mylib`) which has a dependency on another library (`thatlib`). Now, ✅ Getting data from api on app startup?I am experimenting with a very simple app. I want to keep a list of pokemon that I get from an api i✅ Xaml Grid unitsDoes someone know why using the star means the row takes up as much space as possible., why would th❔ Trying to figure out how to write a simple scriptI'm fairly new to c# and I'm trying to figure out how to write this question in a for loop. it goes ❔ Update all packages in project or slnWith the Nuget Package Manager console, you can update all packages in a project (or solution I thin