Yes the solution is 1 because the end is not a valid gene, since it is not present in the bank. Check the question which says the start is valid gene since not included in the bank but the end does not have to be valid. Thus return 1
v.ankit001
@v.ankit001
Posts made by v.ankit001

RE: test case problem

Easy to understand, python BFS, graph on bank nodes
The problem can be solved by making graph on bank nodes, and also inserting start in this graph. Since end is already present in the bank we dont need to add end explicitly here. We mark an edge between two words if there is change of only one character in these two words, which is what hasEdge check. The edge is added in both the directions since it is undirected graph. Then we edges for start node. Once the graph is complete, just call BFS to count the number of steps to return the mutation.
class Solution(object): def minMutation(self, start, end, bank): """ :type start: str :type end: str :type bank: List[str] :rtype: int """ def hasEdge(s, t): if len(s) != len(t): return False count = 0 for i, c in enumerate(s): if c != t[i]: count += 1 return count == 1 def bfs(start, end, graph, visited): q = collections.deque() q.append(start) q.append('#') visited[start] = 1 step = 0 while q: node = q.popleft() if node == "#": if not q: break q.append('#') step += 1 for neigh in list(graph[node]): if not visited[neigh]: visited[neigh] = 1 q.append(neigh) if neigh == end: return step+1 return 1 if not bank or end not in bank: return 1 if start == end: return 0 graph, n = collections.defaultdict(set), len(bank) for i in xrange(n): for j in xrange(i+1, n): if hasEdge(bank[i], bank[j]): graph[bank[i]].add(bank[j]) graph[bank[j]].add(bank[i]) visited = {} visited[start], visited[end] = 0, 0 for i in xrange(n): visited[bank[i]] = 0 if hasEdge(start, bank[i]): graph[start].add(bank[i]) graph[bank[i]].add(start) return bfs(start, end, graph, visited)

One liner python
class Solution(object): def findDuplicates(self, nums): """ :type nums: List[int] :rtype: List[int] """ return [no for no, count in collections.Counter(nums).items() if count > 1]

Python solution with memorization, easy to understand for beginners //
def findmax(self, root, ss, memory):
if not root: return 0 if (root,ss) in memory: return memory[(root,ss)] mx = root.val if ss else 0 l = self.findmax(root.left, 0, memory) r = self.findmax(root.right, 0, memory) ll = self.findmax(root.left, 1, memory) rr = self.findmax(root.right, 1, memory) if ss: mx += l + r else: mx += max(l+r, l+rr, ll+r, ll+rr) memory[(root,ss)] = mx return mx def rob(self, root): """ :type root: TreeNode :rtype: int """ memory = {} return max(self.findmax(root, 0, memory), self.findmax(root, 1, memory))

RE: What's problem with my python code? Why Time Limit Exceeded?
Try to do this with memorization, the problem can be optimized using dynamic programming because the problem contains similar repeated subproblem calculations.

RE: Why my codes cannot pass all the test cases?
Basically, your compute function had problem in calculating case like 7 3+2, in this case your compute function contains 75=2, but actually you need to take care of the integer sign also, so it should compute 71=6. That is why you need to take asign and bsign, and compute the result accordingly and store the sign again back to op list.

RE: Why my codes cannot pass all the test cases?
Slight modification to your code which works fine. Please check the below code:
def compute(self, num, op): a, b=num.pop(), num.pop() l=len(op) asign=op.pop() if l==1 or op[1]=="(": if asign=="+": num.append(a+b) elif asign=="": num.append(ba) return bsign= op.pop() if asign == bsign: num.append(a+b) op.append(asign) elif asign!=bsign: if a > b: num.append(ab) op.append(asign) elif b > a: num.append(ba) op.append(bsign) def calculate(self, s): """ :type s: str :rtype: int """ num, op=[],[] n=len(s) tmp="" for i in range(n): if s[i].isdigit(): tmp+=s[i] if i==n1 or not s[i+1].isdigit(): num.append(int(tmp)) tmp="" elif s[i] in "(+": op.append(s[i]) elif s[i]==")": while op[1]!="(": self.compute(num,op) op.pop() while op: self.compute(num, op) return num[1]