SECTION
Published 2019. 10. 27. 23:48
[Codewar] Find the missing letter PS/Codewar

https://www.codewars.com/kata/5839edaa6754d6fec10000a2/train/java

 

Codewars: Train your coding skills

Codewars is where developers achieve code mastery through challenge. Train on kata in the dojo and reach your highest potential.

www.codewars.com

 

Instructions

#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'

["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!

 

 

Solution

import java.util.Arrays;

public class Kata
{
  static char[]alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  static char[]ALPHA = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
	
  public static char findMissingLetter(char[] array)
	  {
		
		int[]result = new int[array.length];
    
		for(int i=0; i<array.length; i++) {
			if(Character.isLowerCase(array[i])==true)
			result[i] = Arrays.binarySearch(alpha, array[i]);
			else if(Character.isUpperCase(array[i])==true)
			result[i] = Arrays.binarySearch(ALPHA, array[i]);
		}
		
		int j=0;
        
		for(int c:result) {
			if((result[j+1]) != c+1) {
			   if(Character.isLowerCase(array[j])==true)
			   return alpha[result[j]+1];
			   else if(Character.isUpperCase(array[j])==true)
			   return ALPHA[result[j]+1];
		   }
		   j++;
		}
	  }
		
	    return ' ';
	}
}

 

profile

SECTION

@SectionR0

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그