1 May 2014

Missing Character

Problem Statement:
        Given a input string and  range , find the missing character.

For Example,

Input String:   "bCDfa"
Range : 'a' to 'f''

Therefore missing character is 'e'.

Solution:
Run Source Code
      We will be using hashtable concept here. Below is the implementation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Given Input String and range find the missing character 
 * @author Prateek
 */
public class MissingChar {

 private static final int offset = 97;
 private static final int PRESENT = 1;
 private static final int BLANK = 32;

 public static char missingChar(String input, char startChar, char endChar) {
  if (input == null || input.equals(""))
   return (char) BLANK;

  int start = (int) startChar, end = (int) endChar;

  int[] hash = new int[end - start+1];
        input=input.toLowerCase();
        
        int i=0;
        for(;i<input.length();i++){
         int item =(int)input.charAt(i);
         hash[item - offset]=PRESENT;
        }
        i=0;
        for(;i<hash.length;i++){
         if(hash[i]!=PRESENT)
          return (char)(i+offset);
        }
  return (char)BLANK;
 }
 
 public static void main(String[] args) {
  String s="bcADf";
  
  System.out.println(missingChar(s, 'a', 'f'));
  
 }
}

Please comment and post your suggestions.
Happy Coding !!:)