簡単 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 つずつ開いてみましょう。

ヒント 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 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。