Medium

Given a string, return its encoding defined as follows:

  • First, the string is divided into the least possible number of disjoint substrings consisting of identical characters
    • for example, "aabbbc" is divided into ["aa", "bbb", "c"]
  • Next, each substring with length greater than one is replaced with a concatenation of its length and the repeating character
    • for example, substring "bbb" is replaced by "3b"
  • Finally, all the new strings are concatenated together in the same order and a new string is returned.

Example

For s = "aabbbc", the output should be
lineEncoding(s) = "2a3bc".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string s

    String consisting of lowercase English letters.

    Guaranteed constraints:
    4 ≤ s.length ≤ 15.

  • [output] string

    • Encoded version of s.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char *lineEncoding(char *s)
{
	int count=0, current=0;
	char *encoding=(char *)calloc((strlen(s)+1),sizeof(char));

	for(int i=0;i<strlen(s);i++,count++)
	{
		if(strlen(s)==1)
			return s;

		if(i==0)
			continue;

		if(s[i]!=s[i-1] || i==strlen(s)-1)
		{
			if(s[i]!=s[i-1] && i==strlen(s)-1)
			{
				if(count==1)
					encoding[current++]=s[i-1];
				else
				{
					if(count>9)
						encoding[current++]='0'+(count/10);
					encoding[current++]='0'+(count%10);
					encoding[current++]=s[i-1];
				}

				encoding[current++]=s[i];
			}
			else if(i==strlen(s)-1)
			{
				count++;

				if(count>9)
					encoding[current++]='0'+(count/10);
				encoding[current++]='0'+(count%10);
				encoding[current++]=s[i];
			}
			else
			{
				if(count==1)
					encoding[current++]=s[i-1];
				else
				{
					if(count>9)
						encoding[current++]='0'+(count/10);
					encoding[current++]='0'+(count%10);
					encoding[current++]=s[i-1];
				}
				count=0;
			}
		}
	}

	return encoding;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> deleteDigit  (0) 2020.04.06
<Codesignal> chessKnight  (0) 2020.04.06
<Codesignal> isDigit  (0) 2020.04.06
<Codesignal> Is MAC48 Address?  (0) 2020.04.06
<Codesignal> Elections Winners  (0) 2020.04.06

Easy

Determine if the given character is a digit or not.

Example

  • For symbol = '0', the output should be
    isDigit(symbol) = true;
  • For symbol = '-', the output should be
    isDigit(symbol) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] char symbol

    A character which is either a digit or not.

    Guaranteed constraints:
    Given symbol is from ASCII table.

  • [output] boolean

    • true if symbol is a digit, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isDigit(char symbol)
{
	return isdigit(symbol);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> chessKnight  (0) 2020.04.06
<Codesignal> lineEncoding  (0) 2020.04.06
<Codesignal> Is MAC48 Address?  (0) 2020.04.06
<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05

Medium

A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.

The standard (IEEE 802) format for printing MAC-48 addresses in human-friendly form is six groups of two hexadecimal digits (0 to 9 or A to F), separated by hyphens (e.g. 01-23-45-67-89-AB).

Your task is to check by given string inputString whether it corresponds to MAC-48 address or not.

Example

  • For inputString = "00-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = true;
  • For inputString = "Z1-1B-63-84-45-E6", the output should be
    isMAC48Address(inputString) = false;
  • For inputString = "not a MAC-48 address", the output should be
    isMAC48Address(inputString) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    Guaranteed constraints:
    15 ≤ inputString.length ≤ 20.

  • [output] boolean

    • true if inputString corresponds to MAC-48 address naming rules, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isMAC48Address(char *inputString)
{
	if(strlen(inputString)!=17)
		return false;

	for(int i=0;i<strlen(inputString);i++)
		if(!(i%3==2?inputString[i]=='-':isdigit(inputString[i])||'A'<=inputString[i]&&'F'>=inputString[i]))
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> lineEncoding  (0) 2020.04.06
<Codesignal> isDigit  (0) 2020.04.06
<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> isBeautifulString  (0) 2020.04.05

Medium

Elections are in progress!

Given an array of the numbers of votes given to each of the candidates so far, and an integer k equal to the number of voters who haven't cast their vote yet, find the number of candidates who still have a chance to win the election.

The winner of the election must secure strictly more votes than any other candidate. If two or more candidates receive the same (maximum) number of votes, assume there is no winner at all.

Example

For votes = [2, 3, 5, 2] and k = 3, the output should be
electionsWinners(votes, k) = 2.

  • The first candidate got votes. Even if all of the remaining 3 candidates vote for him, he will still have only 5 votes, i.e. the same number as the third candidate, so there will be no winner.
  • The second candidate can win if all the remaining candidates vote for him (3 + 3 = 6 > 5).
  • The third candidate can win even if none of the remaining candidates vote for him. For example, if each of the remaining voters cast their votes for each of his opponents, he will still be the winner (the votes array will thus be [3, 4, 5, 3]).
  • The last candidate can't win no matter what (for the same reason as the first candidate).

Thus, only 2 candidates can win (the second and the third), which is the answer.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] array.integer votes

    A non-empty array of non-negative integers. Its ith element denotes the number of votes cast for the ith candidate.

    Guaranteed constraints:
    4 ≤ votes.length ≤ 10^5,
    0 ≤ votes[i] ≤ 10^4.

  • [input] integer k

    The number of voters who haven't cast their vote yet.

    Guaranteed constraints:
    0 ≤ k ≤ 10^5.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

// Arrays are already defined with this interface:
// typedef struct arr_##name {
//   int size;
//   type *arr;
// } arr_##name;
//
// arr_##name alloc_arr_##name(int len) {
//   arr_##name a = {len, len > 0 ? malloc(sizeof(type) * len) : NULL};
//   return a;
// }
//
//
int electionsWinners(arr_integer votes,int k)
{
	int max=0, count=0;

	for(int i=0;i<votes.size;i++)
		max=votes.arr[i]>max?votes.arr[i]:max;

	if(k==0)
	{
		for(int i=0;i<votes.size;i++)
			count+=votes.arr[i]==max;

		return count==1;
	}

	for(int i=0;i<votes.size;i++)
		count+=votes.arr[i]+k>max;

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isDigit  (0) 2020.04.06
<Codesignal> Is MAC48 Address?  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05

Medium

Given a string, find the shortest possible string which can be achieved by adding characters to the end of initial string to make it a palindrome.

Example

For st = "abcdc", the output should be
buildPalindrome(st) = "abcdcba".

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string st

    A string consisting of lowercase English letters.

    Guaranteed constraints:
    3 ≤ st.length ≤ 10.

  • [output] string

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

char *buildPalindrome(char *st)
{
	int N=strlen(st);

	for(int i=0;i<N;i++)
	{
		bool palindrome=true;

		if(st[i]==st[N-1])
		{
			for(int j=1;i+j<N-1;j++)
				if(st[i+j]!=st[N-j-1])
				{
					palindrome=false;
					break;
				}

			if(palindrome)
			{
				for(int j=i-1;j>=0;j--)
					st[N++]=st[j];

				st[N]='\0';
				return st;
			}
		}
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Is MAC48 Address?  (0) 2020.04.06
<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05

Medium

A string is said to be beautiful if each letter in the string appears at most as many times as the previous letter in the alphabet within the string; ie: b occurs no more times than a; c occurs no more times than b; etc.

Given a string, check whether it is beautiful.

Example

  • For inputString = "bbbaacdafe", the output should be

     isBeautifulString(inputString) = true.

    This string contains 3 as, 3 bs, 1 c, 1 d, 1 e, and 1 f (and 0 of every other letter), so since there aren't any letters that appear more frequently than the previous letter, this string qualifies as beautiful.

  • For inputString = "aabbb", the output should be 

    isBeautifulString(inputString) = false.

    Since there are more bs than as, this string is not beautiful.

  • For inputString = "bbc", the output should be 

    isBeautifulString(inputString) = false.

    Although there are more bs than cs, this string is not beautiful because there are no as, so therefore there are more bs than as.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string inputString

    A string of lowercase English letters.

    Guaranteed constraints:
    3 ≤ inputString.length ≤ 50.

  • [output] boolean

    • Return true if the string is beautiful, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool isBeautifulString(char *inputString)
{
	int alphabet[26]={0, };

	for(int i=0;i<strlen(inputString);i++)
		alphabet[inputString[i]-'a']++;
	for(int i=1;i<26;i++)
		if(alphabet[i-1]<alphabet[i])
			return false;

	return true;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Elections Winners  (0) 2020.04.06
<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05

Easy

Given the positions of a white bishop and a black pawn on the standard chess board, determine whether the bishop can capture the pawn in one move.

The bishop has no restrictions in distance for each move, but is limited to diagonal movement. Check out the example below to see how it can move:

Example

  • For bishop = "a1" and pawn = "c3", the output should be
    bishopAndPawn(bishop, pawn) = true.

     

  • For bishop = "h1" and pawn = "h3", the output should be
    bishopAndPawn(bishop, pawn) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string bishop

    Coordinates of the white bishop in the chess notation.

    Guaranteed constraints:
    bishop.length = 2,
    'a' ≤ bishop[0] ≤ 'h',
    1 ≤ bishop[1] ≤ 8.

  • [input] string pawn

    Coordinates of the black pawn in the same notation.

    Guaranteed constraints:
    pawn.length = 2,
    'a' ≤ pawn[0] ≤ 'h',
    1 ≤ pawn[1] ≤ 8.

  • [output] boolean

    • true if the bishop can capture the pawn, false otherwise.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

bool bishopAndPawn(char *bishop,char *pawn)
{
	return abs(pawn[0]-bishop[0])==abs(pawn[1]-bishop[1]);
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> buildPalindrome  (0) 2020.04.05
<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> digitDegree  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05

Medium

Let's define digit degree of some positive integer as the number of times we need to replace this number with the sum of its digits until we get to a one digit number.

Given an integer, find its digit degree.

Example

  • For n = 5, the output should be
    digitDegree(n) = 0;
  • For n = 100, the output should be
    digitDegree(n) = 1.
    1 + 0 + 0 = 1.
  • For n = 91, the output should be
    digitDegree(n) = 2.
    9 + 1 = 10 -> 1 + 0 = 1.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    5 ≤ n ≤ 10^9.

  • [output] integer

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int digitDegree(int n)
{
	int count=0;

	while(n%10!=n)
	{
		int N=0;

		while(n>0)
		{
			N+=n%10;
			n/=10;
		}
		n=N;

		count++;
	}

	return count;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> isBeautifulString  (0) 2020.04.05
<Codesignal> Bishop and Pawn  (0) 2020.04.05
<Codesignal> longestDigitsPrefix  (0) 2020.04.05
<Codesignal> Knapsack Light  (0) 2020.04.05
<Codesignal> growingPlant  (0) 2020.04.05

+ Recent posts