Is Unstable Pair?

Description:

Some file managers sort filenames taking into account case of the letters, others compare strings as if all of the letters are of the same case. That may lead to different ways of filename ordering.

Call two filenames an unstable pair if their ordering depends on the case.

To compare two filenames a and b, find the first position i at which a[i] ≠ b[i]. If a[i] < b[i], then a < b. Otherwise a > b. If such position doesn’t exist, the shorter string goes first.

Given two filenames, check whether they form an unstable pair.

Example

  • For filename1 = "aa" and filename2 = "AAB", the output should be
    isUnstablePair(filename1, filename2) = true.

    Because "AAB" < "aa", but "AAB" > "AA".

  • For filename1 = "A" and filename2 = "z", the output should be
    isUnstablePair(filename1, filename2) = false.

    Both "A" < "z" and "a" < "z".

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string filename1

    A non-empty string of letters and digits.

    Constraints:
    1 ≤ filename1.length ≤ 10.

  • [input] string filename2

    A non-empty string of letters and digits. It’s guaranteed that there is no ambiguity, i.e. even being considered in the same case strings are never equal.

    Constraints:
    1 ≤ filename2.length ≤ 10.

  • [output] boolean

    true if filename1 and filename2 form an unstable pair, false otherwise.

Tests:
Solution:

bool isUnstablePair(string filename1, string filename2) {
    if(string.Compare(filename2,filename1, StringComparison.Ordinal)>0) {
        return string.Compare(filename2.ToUpper(),filename1.ToUpper(),StringComparison.Ordinal)<0;
    }
    else if(string.Compare(filename2,filename1, StringComparison.Ordinal)<0) {
        return string.Compare(filename2.ToLower(),filename1.ToLower(), StringComparison.Ordinal)>0;
    }
    else 
        return false;
}
Advertisements

Is MAC48 Address?

Description:

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).

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

  • [time limit] 3000ms (cs)
  • [input] string inputString

    Constraints:
    15 ≤ inputString.length ≤ 20.

  • [output] boolean

    true if inputString corresponds to MAC-48 address naming rules, falseotherwise.

Tests:
Solution:

bool isMAC48Address(string inputString) {
    for (int i = 0; i < inputString.Length; i++) {
        if (i % 3 == 2) {
            if (inputString[i] != '-') {
                return false;
            }
        } else {
            char sym = inputString[i];
            if (!('0' <= sym && sym <= '9' || 'A' <= sym && sym <= 'F')) {
                return false;
            }
        }
    }

    return inputString.Length == 17;
}

HTML End Tag By Start Tag

Description:

You are implementing your own HTML editor. To make it more comfortable for developers you would like to add an auto-completion feature to it.

Given the starting HTML tag, find the appropriate end tag which your editor should propose.

If you are not familiar with HTML, consult with this note.

Example

  • For startTag = "", the output should be
    htmlEndTagByStartTag(startTag) = "";
  • For startTag = "", the output should be
    htmlEndTagByStartTag(startTag) = "
    ".

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string startTag

    Constraints:
    3 ≤ startTag.length ≤ 75.

  • [output] string

Tests:
Solution:

string htmlEndTagByStartTag(string startTag) {
    String result = "</";
    int position = 1;
    while (position < startTag.Length - 1 && startTag[position] != ' ') {
            result += startTag[position++];
    }
    result += '>';
    return result;
}

Find Email Domain

Description:

An email address such as "John.Smith@example.com" is made up of a local part ("John.Smith"), an "@" symbol, then a domain part ("example.com").

The domain name part of an email address may only consist of letters, digits, hyphens and dots. The local part, however, also allows a lot of different special characters. Here you can look at several examples of correct and incorrect email addresses.

Given a valid email address, find its domain part.

Example

  • For address = "prettyandsimple@example.com", the output should be
    findEmailDomain(address) = "example.com";
  • For address = "[]:,;@\"!#$%&*+-/=?^_{}| ~.a\"@example.org", the output should be
    findEmailDomain(address) = "example.org".

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string address

    Constraints:
    10 ≤ address.length ≤ 50.

  • [output] string

Tests:
Solution:

string findEmailDomain(string address) {
    var ans = address.Split('@');
    return ans[ans.Length - 1];
}

Is Case-Insensitive Palindrome?

Description:

Given a string, check if it can become a palindrome through a case change of some (possibly, none) letters.

Example

  • For inputString = "AaBaa", the output should be
    isCaseInsensitivePalindrome(inputString) = true.

    "aabaa" is a palindrome as well as "AABAA", "aaBaa", etc.

  • For inputString = "abac", the output should be
    isCaseInsensitivePalindrome(inputString) = false.

    All the strings which can be obtained via changing case of some group of letters, i.e. "abac", "Abac", "aBAc" and 13 more, are not palindromes.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string inputString

    Non-empty string consisting of letters.

    Constraints:
    4 ≤ inputString.length ≤ 10.

  • [output] boolean

Tests:
Solution:

bool isCaseInsensitivePalindrome(string inputString) {
    for (int i = 0; i < inputString.Length / 2; i++) {
        char[] c = {
            inputString[i],
            inputString[inputString.Length - i - 1]
        };
        for (int j = 0; j < 2; j++) {
            if (c[j] >= 'a' && c[j] <= 'z') {
                c[j] = (char) (c[j] - 'a' + 'A');
            }
        }
        if (c[0] != c[1]) {
            return false;
        }
    }

    return true;
}

Is Tandem Repeat?

Description:

Determine whether the given string can be obtained by one concatenation of some string to itself.

Example

  • For inputString = "tandemtandem", the output should be
    isTandemRepeat(inputString) = true;
  • For inputString = "qqq", the output should be
    isTandemRepeat(inputString) = false;
  • For inputString = "2w2ww", the output should be
    isTandemRepeat(inputString) = false.

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string inputString

    Constraints:
    3 ≤ inputString.length ≤ 15.

  • [output] boolean

    true if inputString represents a string concatenated to itself, falseotherwise.

Tests:
Solution:

bool isTandemRepeat(string inputString) {
    if(inputString.Length==1 || inputString.Length%2==1)
        return false;
    string subString = inputString.Substring(0,inputString.Length/2);
    string temp2 = inputString.Replace(subString,"");
    if(String.IsNullOrEmpty(temp2))
        return true;
    return false;
}

Proper Noun Correction

Description:

Proper nouns always begin with a capital letter, followed by small letters.

Correct a given proper noun so that it fits this statement.

Example

  • For noun = "pARiS", the output should be
    properNounCorrection(noun) = "Paris";
  • For noun = "John", the output should be
    properNounCorrection(noun) = "John".

Input/Output

  • [time limit] 3000ms (cs)
  • [input] string noun

    A string representing a proper noun with a mix of capital and small Latin letters.

    Constraints:
    1 ≤ noun.length ≤ 10.

  • [output] string

    Corrected (if needed) noun.

Tests:
Solution:

string properNounCorrection(string noun) {
    var first = noun.Substring(0,1).ToUpper();
    var last = noun.Substring(1).ToLower();
    return first + last;
}