STRANGE: C# code failed the "a", "c", ["a","b","c"] case in OJ, but passed in VS2012


  • 9
    G

    The following code output '2' in VS 2012 for the test case below (please free to try it). But OJ kept output '0', so failed.

    Input: "a", "c", ["a","b","c"]
    Output: 0
    Expected: 2

    What's the problem?

    Thanks!

    public class Solution
            {
                public int LadderLength(string beginWord, string endWord, ISet<string> wordDict)
                {
                    Queue<string> q = new Queue<string>();
                    q.Enqueue(beginWord);
                    int qcc = 1, qcn = 0, N = beginWord.Length, level = 1;
                    while (q.Count() > 0)
                    {
                        string qf = q.Peek();
                        q.Dequeue();
                        qcc--;
                        //varying by 1
                        string qf0 = qf;
                        for (int i = 0; i < N; i++)
                        {
                            //char c=qf[i];
                            for (int j = 0; j < 26; j++)
                            {
                                if (qf[i] == (char)('a' + j))
                                    continue;
                                string sc = "";
                                sc += (char)('a' + j); //sick of this
                                string s2 = qf.Insert(i, sc).Remove(i + 1, 1);
                                if (wordDict.Contains(s2))
                                {
                                    if (s2 == endWord)
                                        return level + 1; //+1!
                                    q.Enqueue(s2);
                                    wordDict.Remove(s2);
                                    qcn++;
                                }
                                
                            }
                            //qf[i] = c;
                            qf = qf0;
                        }
                        if (qcc == 0)
                        {
                            qcc = qcn;
                            qcn = 0;
                            level++;
                        }
                    }
                    return 0;
                }
            }
            class Program
            {
                static void Main(string[] args)
                {
                    Solution sl = new Solution();
                    HashSet<string> hs3 = new HashSet<string> { "a", "b", "c" };
                    int yyue = sl.LadderLength("a", "c", hs3);
                }
            }

  • 2
    A

    I am facing the similar problem. The reason for the same is that (wordDict.Contains(s2)) results in always false. To confirm the same , i checked in case wordList is ["a","b","c"] for "a" and the result was false. However , the same code returns true in VS 2012. As per my opinion , the object used to create an ISet is different from HashSet. As if it is a HashSet , the code works fine.

    To confirm the above i overwritten the wordDict with wordDict = new HashSet<string>{"a","b","c"} in the function itself. The response was 2 but it failed in another test case as dictionary was overwritten inside.

    Further to debug , tried to print the wordDict and it was empty. It appears to be a bug in the leetcode where in the main function is passing the dictionary as empty. That is the reason for the output as 0


  • 0
    C

    it seems for this case, if try to return wordList.Count, it always 0.
    e.g.

    public int LadderLength(string beginWord, string endWord, ISet<string> wordList)
    {
    Queue<string> q = new Queue<string>();
    q.Enqueue(beginWord);
    return wordList.Count;
    }

    the output always 0


  • 1
    R

    Add these lines in the begining

    
            wordList.add(endWord);
            wordList.remove(beginWord);
    
    
    

  • 0
    A

    Indeed, this is a bug in the test case, the wordList is empty.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.