❔ Only one testcase failed
I am stuck with this puzzle https://leetcode.com/problems/decode-string/submissions/
My code fails at this specific testcase as per screenshot.
Could anyone kindly point me in the right direction?
`
My code fails at this specific testcase as per screenshot.
Could anyone kindly point me in the right direction?
public class DecodeString
{
public string StringDecode(string s)
{
var stkChar = new Stack<char>();
var stkDigit = new Stack<int>();
int count = 0;
string recorded = "";
string currStr = "";
for (int i = 0; i < s.Length; i++)
{
if (Char.IsDigit(s[i]))
{
count = count * 10 + s[i] - '0';
Console.WriteLine($"it is a digit, report me {count}");
} else if (s[i] == '[')
{
stkChar.Push(s[i]);
stkDigit.Push(count);
count = 0;
Console.WriteLine("please keep me " + currStr);
recorded = currStr;
currStr = "";
Console.WriteLine($"currStr after opening bracket {currStr}");
} else if (s[i] == ']')
{
int poppedCount = stkDigit.Pop();
//char poppedChar = stkChar.Pop();
string decoded = currStr;
for (int j = 1; j < poppedCount; j++)
{
decoded += currStr;
}
Console.WriteLine($"what is decoded {decoded}");
currStr = decoded; // Update currStr
Console.WriteLine($"currStr after closing bracket {currStr}");
}
else
{
currStr += s[i];
}
}
return recorded + currStr;
}
}public class DecodeString
{
public string StringDecode(string s)
{
var stkChar = new Stack<char>();
var stkDigit = new Stack<int>();
int count = 0;
string recorded = "";
string currStr = "";
for (int i = 0; i < s.Length; i++)
{
if (Char.IsDigit(s[i]))
{
count = count * 10 + s[i] - '0';
Console.WriteLine($"it is a digit, report me {count}");
} else if (s[i] == '[')
{
stkChar.Push(s[i]);
stkDigit.Push(count);
count = 0;
Console.WriteLine("please keep me " + currStr);
recorded = currStr;
currStr = "";
Console.WriteLine($"currStr after opening bracket {currStr}");
} else if (s[i] == ']')
{
int poppedCount = stkDigit.Pop();
//char poppedChar = stkChar.Pop();
string decoded = currStr;
for (int j = 1; j < poppedCount; j++)
{
decoded += currStr;
}
Console.WriteLine($"what is decoded {decoded}");
currStr = decoded; // Update currStr
Console.WriteLine($"currStr after closing bracket {currStr}");
}
else
{
currStr += s[i];
}
}
return recorded + currStr;
}
}
LeetCode
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.
