Loading Now

reverseInverse

Given a string s, your mission is to apply the following easy-to-comprehend algorithm to it:

  1. Find all the words in s, where a word is a sequence of consecutive alphanumeric characters with no other letters around it;
  2. Reverse the characters in each word;
  3. For each word, swap the cases of its characters so that the case of a character at each position differs from the case at the corresponding position of the original (unreversed) word.

Return the obtained string as the answer.

Example:

  • For s = "So, what is CodeLearn?", the answer should be reverseInverse(s) = "oS, TAHW SI nRAElEDOC?".

There are 4 words in s: "So", "what", "is", and "CodeLearn". Let’s take the word "CodeLearn" as an example:

  • The letters 'C' at index 0 and letter 'L' at index 4 are uppercase, while all the other letters are lowercase;
  • "codelearn" reversed becomes "nraeledoc";
  • With the cases swapped, the letters at indices 0 and 4 should be lowercase and all the other letters should be uppercase;
  • Thus, the final word is "nRAElEDOC".

Input/Output:

  • [execution time limit] 0.5 seconds 

  • [input] string s

    A string containing only alphanumeric characters and punctuation marks.

    Guaranteed constraints:
    0 ≤ s.length < 500.

  • [output] string

    • The result of applying the algorithm described above to s.

Post Comment

Contact