C
C#10mo ago
morry329#

❔ 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?
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
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.
No description
3 Replies
JakenVeina
JakenVeina10mo ago
so, you're just supposed to be parsing that input commandbas a set of instructions for generating the output? I very much like that I'm not seeing recusrion I'm going to recommend you model the problem a little more at the expense of having an "efficient" solution which is really how you should do most development: implement sensibly first, then optimize where you can so if we think about how to "model" this your input consists of a string, that contains a set of instructions write me a class that represents one of those instructions or a set of classes, if that makes more sense
Moods
Moods10mo ago
ended up doing it out of curiosity; took me longer than id like to admit. if this helps here is my approach and im able to explain any part you have questions about(also sorry Jaken I HAD to use recursion i am not thinking of a solution without one)
public class Solution {

private HashSet<int> hashset;

public string DecodeString(string s)
{
hashset = new HashSet<int>();

var res = Helper(0, s);

return res;
}

private string Helper(int start, string str)
{
var _res = new StringBuilder();

for(int a = start; a < str.Length; a++)
{
if(str[a] == ']' && !hashset.Contains(a))
{
hashset.Add(a);
break;
}

if(Char.IsLetter(str[a]) && !hashset.Contains(a))
{
hashset.Add(a);
_res.Append(str[a]);
}
else{
if(Char.IsNumber(str[a]) && !hashset.Contains(a))
{
string _num = "";
while(str[a] != '[')
{
_num += str[a];
hashset.Add(a);
a++;
}
a--;
int num = Int32.Parse(_num);
string temp = Helper(a + 2, str);

string new_temp = Solver(num, temp);

_res.Append(new_temp);
}
}
}
return _res.ToString();
}

private string Solver(int count, string sentence)
{
string res = "";

for(int a = 0; a < count; a++)
res += sentence;

return res;
}
}
public class Solution {

private HashSet<int> hashset;

public string DecodeString(string s)
{
hashset = new HashSet<int>();

var res = Helper(0, s);

return res;
}

private string Helper(int start, string str)
{
var _res = new StringBuilder();

for(int a = start; a < str.Length; a++)
{
if(str[a] == ']' && !hashset.Contains(a))
{
hashset.Add(a);
break;
}

if(Char.IsLetter(str[a]) && !hashset.Contains(a))
{
hashset.Add(a);
_res.Append(str[a]);
}
else{
if(Char.IsNumber(str[a]) && !hashset.Contains(a))
{
string _num = "";
while(str[a] != '[')
{
_num += str[a];
hashset.Add(a);
a++;
}
a--;
int num = Int32.Parse(_num);
string temp = Helper(a + 2, str);

string new_temp = Solver(num, temp);

_res.Append(new_temp);
}
}
}
return _res.ToString();
}

private string Solver(int count, string sentence)
{
string res = "";

for(int a = 0; a < count; a++)
res += sentence;

return res;
}
}
i feel you can do this with a stack though, as that was my initial thought, but i cant be arsed to think of the logic
Accord
Accord10mo 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.