Different Output from My Local Environment. Please HELP


  • 0
    Z

    The error message I received is:

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

    However, my eclipse does give me a correct answer.

    My code simply goes through the tree level by level and stops going further once it finds the answer.

    Why is it generating a different result here?

    public class Solution {
    
     public static class Node{
        	String value;
        	List<Node> connected;
        	Node parent = null;
        	int level = 1;
        	boolean visited = false;
        	
        	public Node(String v, List<Node> c){
        		this.value = v;
        		this.connected = c;
        	}
        	
        	public Node(String v, Node parent){
        		this.value = v;
        		this.parent = parent;
        		connected = new ArrayList<>();
        	}
        	
        	public Node(String start, int level) {
    			// TODO Auto-generated constructor stub
        		this.value = start;
        		this.level = level;
    		}
    
    		@Override
        	public boolean equals(Object o){
        		if (o instanceof Node){
        			Node mo = (Node) o;
        			return mo.value.equals(this.value);
        		}
        		return false;
        	}
        	
            @Override
            public int hashCode() {
                   final int prime = 31;
                   int result = 1;
                   result = prime * value.hashCode();
                   return result;
            }
    
        	@Override
        	public String toString(){
        		
        		String r = "value: " + value + ", parent: " + (parent == null ? "null" : parent.value) + ", level: " + level + ", connected: [";
        		for (Node j : connected){
        			r = r + j.value + " ";
        		}
        		r = r+ "]";
        		return "[" + r + "]";
        	}
        }
    
    Map<String, Node> map = new HashMap<>(); 
     
    public boolean isOneLetterDiff(String a, String b){
    	if (a.length() != b.length()){
    		return false;
    	}else{
    		int diff = 0;
    		for (int i = 0; i < a.length(); i++){
    			if (a.charAt(i) != b.charAt(i)){
    				diff++;
    			}
    			if (diff >=2){
    				return false;
    			}
    		}
    		return diff == 1;
    	}
    }
    
    public List<Node> dicContainsOneLetterDiff(Map<String, Node> dict, String j){
    	List<Node> r = new ArrayList<>();
    	for (int i = 0; i < j.length(); i++){
    		for (char a = 'a'; a <= 'z'; a++){
    			if(j.charAt(i) != a){
    				String newS = "";
    				if (i == 0 && j.length() > 1){
    					newS = a + j.substring(1);
    				}else if(i == j.length() - 1 ){
    					newS = j.substring(0, i) + a;
    				}else{
    					newS = j.substring(0, i) + a + j.substring(i + 1, j.length());
    				}
    				
    				if (dict.containsKey(newS)){
    					r.add(dict.get(newS));
    				}
    			}
    		}
    	}
    
    	return r;
    }
    
    public List<List<String>> findLadders(String start, String end, Set<String> dict) {
    	dict.add(end); 
    	dict.add(start); 
    	
    	Node firstNode = new Node(start, 1);
    	Queue<Node> level = new LinkedList <>();
    	Queue<Node> nextLevel = new LinkedList <>();
    	level.add(firstNode);
    	
    	List<List<String>> result = new ArrayList<>();
    	List<String> path = new ArrayList<>();
    	
    	Set<String> visited = new HashSet<>();
    	visited.add(firstNode.value);
    	
    	for(String i : dict){
    		map.put(i, new Node(i, 1));
    	}
    	
    	while(!level.isEmpty()){
    		for(Node node : level){
    			List<Node> list = dicContainsOneLetterDiff(map, node.value);
    			for(Node nnode : list){
    				if(!visited.contains(nnode.value)){
    					nnode.parent = node;
    					visited.add(nnode.value);
    					if(nnode.value == end){
    						Node tmp = nnode;
    						while(tmp != null){
    							path.add(0, tmp.value);
    							tmp = tmp.parent;
    						}
    						result.add(path);
    						path = new ArrayList<String>();
    					}else{
    						nextLevel.add(nnode);
    					}
    				}
    			}
    		}
    		if(!result.isEmpty()){
    			nextLevel.clear();
    		}
    		level = nextLevel;
    		nextLevel = new LinkedList<>();
    	}
    	return result;
    }
    }

Log in to reply
 

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