美文网首页
Find the missing letter

Find the missing letter

作者: Magicach | 来源:发表于2017-12-28 11:17 被阅读0次

Find the missing letter

Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.

You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2.
The array will always contain letters in only one case.

Example:

['a','b','c','d','f'] -> 'e'
['O','Q','R','S'] -> 'P'

(Use the English alphabet with 26 letters!)

Have fun coding it and please don't forget to vote and rank this kata! :-)

I have also created other katas. Take a look if you enjoyed this kata!

Good Solution1:

public class Kata
{
  public static char findMissingLetter(char[] array)
  {
    boolean stop = false;
    int i;
    for(i = 1; i < array.length && !stop; i++)
    {
      if (array[i] - array[i-1] != 1)
        stop = true;
    }
    return (char) (array[i-1]-1);
  }
}

Good Soultion2:

public class Kata
{
  public static char findMissingLetter(char[] array){
    char expectableLetter = array[0];
    for(char letter : array){
      if(letter != expectableLetter) break;
      expectableLetter++;
    }
    return expectableLetter;
  }
}

相关文章

网友评论

      本文标题:Find the missing letter

      本文链接:https://www.haomeiwen.com/subject/odvmgxtx.html