Find the missing letter

MagicachIP属地: 浙江
字数 122阅读 551

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;
  }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
0人点赞
Magicach人生如诗如梦,怎奈何,一落棋子,万古成局
总资产1共写了7108字获得4个赞共4个粉丝

推荐阅读更多精彩内容