Medium

Given two version strings composed of several non-negative decimal fields separated by periods ("."), both strings contain equal number of numeric fields. Return true if the first version is higher than the second version and false otherwise.

The syntax follows the regular semver ordering rules:

1.0.5 < 1.1.0 < 1.1.5 < 1.1.10 < 1.2.0 < 1.2.2 < 1.2.10 < 1.10.2 < 2.0.0 < 10.0.0

There are no leading zeros in any of the numeric fields, i.e. you do not have to handle inputs like 100.020.003 (it would instead be given as 100.20.3).

Example

  • For ver1 = "1.2.2" and ver2 = "1.2.0", the output should be
    higherVersion(ver1, ver2) = true;
  • For ver1 = "1.0.5" and ver2 = "1.1.0", the output should be
    higherVersion(ver1, ver2) = false.

Input/Output

  • [execution time limit] 0.5 seconds (c)

  • [input] string ver1

    Guaranteed constraints:
    1 ≤ ver1.length ≤ 15.

  • [input] string ver2

    Guaranteed constraints:
    1 ≤ ver2.length ≤ 15.

  • [output] boolean

[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 higherVersion(char *ver1,char *ver2)
{
	int count_1=0, count_2=0;

	while(count_1<strlen(ver1) && count_2<strlen(ver2))
	{
		int v1=0, v2=0;

		while(isdigit(ver1[count_1]))
		{
			v1*=10;
			v1+=ver1[count_1++]-'0';
		}
		while(isdigit(ver2[count_2]))
		{
			v2*=10;
			v2+=ver2[count_2++]-'0';
		}
		count_1++;
		count_2++;
		if(v1>v2)
			return true;
		else if(v2>v1)
			return false;
		else if(count_1>=strlen(ver1) || count_2>=strlen(ver2))
			return false;
	}
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> Crossword Formation  (0) 2020.12.30
<Codesignal> Decipher  (0) 2020.06.27
<Codesignal> Is Unstable Pair?  (0) 2020.06.26
<Codesignal> Equal Pair of Bits  (0) 2020.06.17
<Codesignal> Different Rightmost Bit  (0) 2020.06.16

+ Recent posts