My code compiles fine on Eclipse, but got compile error here.


  • 0
    Z

    The error message I got is : "Line 123: error: cannot find symbol: class Entry"
    What my code is doing is fairly simple. It creates a graph and then uses BFS to compute the shortest path.
    Please HELP!

    public class Solution {
    
     static class Node{
        	String value;
        	List<Node> connected;
        	
        	public Node(String v, List<Node> c){
        		this.value = v;
        		this.connected = c;
        	}
        	
        	public Node(String v){
        		this.value = v;
        		connected = new ArrayList<>();
        	}
        	
        	@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(){
        		return value;
        	}
        }
    
     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++;
    			}
    		}
    		return diff == 1;
    	}
    }
    
    public List<List<String>> findLadders(String start, String end, Set<String> dict) {
    	dict.add(end);
    	List<List<String>> result = new ArrayList<List<String>>();
    	
    	Stack<String> stack = new Stack<>();
    	stack.push(start);
    	
    	List<String> curPath = new ArrayList<>();
    	
    	while(!stack.isEmpty()){
    		String curWord = stack.pop();
    		curPath.add(curWord);
    		
    		if (curWord == end){
    			result.add(curPath);
    			if (!curPath.isEmpty()){
        			curPath.remove(curPath.size() - 1);
    			}
    		}else{
    	    	for (String i : dict){
    	    		if (!curPath.contains(i) && isOneLetterDiff(curWord, i)){
    	    			stack.push(i);
    	    		}
    	    	}
    		}
    	}
    	return result;
    }
    
    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 Node buildGraph(String start, Set<String> dict){
    	if(dict.contains(start)){
    		dict.remove(start);
    	}
    	
    	Node ns = new Node(start);
    	map.put(start, ns);
    	
    	for(String s : dict){
    		map.put(s, new Node(s));
    	}
    	
    	
    	for(Entry<String, Node> i : map.entrySet()){
    		List<Node> list = dicContainsOneLetterDiff(map, i.getValue().value);
    		for(Node j: list){
    			i.getValue().connected.add(j);
    		}
    	}
    	return ns;
    }
    
    
    public int ladderLength(String start, String end, Set<String> dict){
    	dict.add(end);
    	Node firstNode = buildGraph(start, dict);
    	
    	Set<Node> currentLevel = new HashSet <>();
    	Set<Node> nextLevel = new HashSet <>();
    	List<Node> path = new LinkedList <>();
    	
    	currentLevel.add(firstNode);
    	
    	int dist = 1;
    	while(!currentLevel.isEmpty()){
    		if (currentLevel.contains(new Node(end))){
    			return dist;
    		}else{
    			for (Node j : currentLevel){
    				path.add(j);
    				for (Node n : j.connected){
    					if (!path.contains(n)){
    						nextLevel.add(n);
    					}
    				}
    			}
    			currentLevel = nextLevel;
    			nextLevel = new HashSet <>();
    			dist++;
    		}
    	}
    	return 0;
    }

  • 0
    S

    Change to

    for(Map.Entry<String, Node> i : map.entrySet()){

Log in to reply
 

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