Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Wednesday, February 17, 2016

Leetcode: Summary Ranges


Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].

Runtime Analysis:

Runtime: O(n)

Algorithmic Approach:

This is an easy question. Since the ranges are all continuous integers, we just need traverse the array and compare each element with its previous, if they are not consecutive, the current number belongs to a new range. In my code, the start and end of the current range is maintained. Don't forget to consider a single number range and the slightly different output format required in this question.

Java Code:













































public class Solution { public List<String> summaryRanges(int[] nums) { int start = 0, end = 0; List<String> toRet = new ArrayList<String>(); String str = ""; if(nums.length == 0) return toRet; for(int i = 0; i < nums.length - 1; i++){ // if consecutive if(nums[i] + 1 == nums[i + 1]){ end++; // if the start, adds the string if(i == start){ str = nums[i] + ""; } } // if not consecutive else{ if(start != end){ str += "->" + nums[i]; } else{ str = nums[i] + ""; } toRet.add(str); start = i+1; end = i+1; } } // covers the last element if(start == end){ toRet.add(nums[end]+""); } else{ str += "->" + nums[end]; toRet.add(str); } return toRet; } }

Monday, February 15, 2016

Leetcode: Word Ladder

Difficulty: Medium

Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
1.    Only one letter can be changed at a time
2.    Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:

  • Return 0 if there is no such transformation sequence.
  • All words have the same length.
  • All words contain only lowercase alphabetic characters.

Runtime Analysis:
First, we need to understand the problem and the approach we should take. Please read the Algorithmic Approach first.

Runtime: O(26n) 
Space: O(n) 

Algorithmic Approach:
This question requires a good understanding of BFS. Given an initial word to start with, we need to transform character by character to reach our final word. However, we cannot transform each character directly from our initial to final state because that particular word may not exist in our dictionary. We might have to transform to intermediary states to in order reach our final string.
See Example:
Dictionary = {"cat", "aat", "aag", "aog", "dog"}
Transformation = ["cat" => "aat" => "aag" => "aog" => "dog"]

Since we do not know which words exist in our dictionary, we would have to go through every alphabetical character (a-z) for every index in our string and check if that is a valid state.
The reason we are using BFS instead of DFS is because we are asked the compute the shortest transformation sequence. Due to this fact, we want to visit the neighbors that are closest distance first. 
We can implement BFS by using a Queue. We push our initial node on first. For each iteration, we pop off all elements in the queue (to process all nodes that are x distance away from our start). For each element, we check if it is equal to our final state, if so, we just return the distance. Otherwise, we push each of the node's neighbors back onto the queue.
In order to prevent cycles in the graph traversal, we remove words that we have already visited from the dictionary.

Java Code: 
public class Solution {
    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        
        Queue<String> queue = new LinkedList<String>();
        queue.add(beginWord);
        int count = 1;
        
        while(!queue.isEmpty()){
            
            int size = queue.size();
            
            for(int j = 0; j < size; j++){
                String word = queue.remove();
                
                if(word.equals(endWord)){
                    return count;
                }
                
                for(int i = 0; i < word.length(); i++){
                    for(char c = 'a'; c <= 'z'; c++){
                        if(c != word.charAt(i)){
                            char[] s = word.toCharArray();
                            s[i] = c;
                            String trans = new String(s);
                            
                            if(wordList.contains(trans)){
                                queue.add(trans);
                                wordList.remove(trans);
                            }
                        }
                    }
                }
            }
            
            count++;
        }
        
        return 0;
    }
}