쉬움 0명 해결 정답률 0%

Word Scramble

Scramble words by swapping each vowel with the consonant right after it, scanning left to right.

배우게 될 내용

  • Walk through a string with a manual index
  • Swap two adjacent characters
  • Skip ahead after a swap

문제

번역본 보기

A word puzzle app makes easy scrambles with one rule. Scan each word from left to right. Whenever a vowel (a, e, i, o, u) is directly followed by a consonant (any other letter), swap the two letters and continue scanning after the pair, so a letter is never moved twice. Otherwise move on by one letter.

For example orange: o and r swap to give ro, then scanning continues at a, which swaps with n, then at g, which is not a vowel, and e is the last letter: ronage.

Implement word_scramble(words).

Input

words: The words, in lowercase letters.

Output

Return the scrambled words in the same order. A word with no vowel followed by a consonant comes back unchanged.

제약 조건

0 ≤ number of words ≤ 1,000

1 ≤ word length ≤ 30

예시

예시 1
입력:
words = ["orange", "apple"]

예상 출력:
expected = ["ronage", "paple"]
입력:
std::vector<std::string> words = {"orange", "apple"};

예상 출력:
std::vector<std::string> expected = {"ronage", "paple"};
입력:
string[] words = new string[] {"orange", "apple"};

예상 출력:
string[] expected = new string[] {"ronage", "paple"};

설명

orange becomes ronage as shown above. In apple, a swaps with p to give pa, then p, l and e stay: paple.

예시 2
입력:
words = ["sky", "tea", "eat"]

예상 출력:
expected = ["sky", "tea", "eta"]
입력:
std::vector<std::string> words = {"sky", "tea", "eat"};

예상 출력:
std::vector<std::string> expected = {"sky", "tea", "eta"};
입력:
string[] words = new string[] {"sky", "tea", "eat"};

예상 출력:
string[] expected = new string[] {"sky", "tea", "eta"};

설명

sky has no vowels and tea has no vowel before a consonant, so both stay. In eat, e is followed by the vowel a, then a swaps with t: eta.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Work on a mutable copy of the word, like a character array.

힌트 2

After a swap, move the index forward by 2 so the moved letters are not looked at again.

힌트 3

Without a swap, move forward by 1.

예상 복잡도

O(total letters) time, O(longest word) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.