Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find missing letter in list of alphabets

Tags:

javascript

I am trying to solve the following issue:

Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.

the inputs that I will get as strings are:

  • abce (Which should return d)
  • bcd (which should return undefined)
  • abcdefghjklmno (which should return i)
  • yz (which should return undefined)

my code currently looks like this:

  function fearNotLetter(str) {
  //create alphabet string
  //find starting letter in alphabet str, using str
  //compare letters sequentially
  //if the sequence doesn't match at one point then return letter
  //if all letters in str appear then return undefined

  var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
  var i = 0;
  var j = 0;
  while (i<alphabet.length && j<str.length) {
    i++;
    if (alphabet.charCodeAt(i) === str.charCodeAt(j)) {
      i++;
      j++;      
    }
    else if (alphabet.charCodeAt(i) !== str.charCodeAt(j)) {
      i++;
      j++;
      if (alphabet.charCodeAt(i) === str.charCodeAt(j-1)) {
        return alphabet.charCodeAt(i-1);  
      }
    }
  }
}

fearNotLetter('abce');

Thanks for your help as always!

like image 764
smeloa Avatar asked Dec 04 '22 03:12

smeloa


2 Answers

I would do it like this:

function fearNotLetter(str) {
    var i, j = 0, m = 122;
    if (str) {
        i = str.charCodeAt(0);
        while (i <= m && j < str.length) {
            if (String.fromCharCode(i) !== str.charAt(j)) {
                return String.fromCharCode(i);
            }
            i++; j++;
        }
    }
    return undefined;
}

console.log(fearNotLetter('abce'));        // "d"
console.log(fearNotLetter('bcd'));         // undefined
console.log(fearNotLetter('bcdefh'));      // "g"
console.log(fearNotLetter(''));            // undefined
console.log(fearNotLetter('abcde'));       // undefined
console.log(fearNotLetter('abcdefghjkl')); // "i"

i can go from 97 to 122, this interval corresponds to the ASCII codes of the lower case alphabet.

If you want it not to be case sensitive, just do str = str.toLowerCase() at the beginning of the function.

like image 57
blex Avatar answered Dec 09 '22 13:12

blex


I think this is the simplest code to do this:

function skippedLetter(str) {
    for (var i = 0; i < str.length - 1; i++) {
        if (str.charCodeAt(i + 1) - str.charCodeAt(i) != 1) {
            return String.fromCharCode(str.charCodeAt(i) + 1);
        }
    }
}

alert(skippedLetter('abce'));

This version will reject illegal input, accept both upper and lower case, check that there is only 1 hole in the range, and that there is exactly 1 character missing.

function skippedLetter(str) {
    if (!str.match(/^[a-zA-Z]+$/)) return;
    var letter = "", offset = str.charCodeAt(0);
    for (var i = 1; i < str.length; i++) {
        var diff = str.charCodeAt(i) - i - offset;
        if (diff == 1) letter += String.fromCharCode(i + offset++)
        else if (diff) return;
    }
    if (letter.length == 1) return letter;
}

alert(skippedLetter('123567'));		// illegal characters
alert(skippedLetter(''));		// empty string
alert(skippedLetter('a'));		// too short
alert(skippedLetter('bc'));		// nothing missing
alert(skippedLetter('df'));		// skipped letter = e
alert(skippedLetter('GHIKLM'));		// skipped letter = J
alert(skippedLetter('nOpRsT'));		// cases mixed
alert(skippedLetter('nopxyz'));		// too many characters missing
alert(skippedLetter('abcefgijk'));	// character missing more than once
alert(skippedLetter('abcefgfe'));	// out of order
like image 31