ง่าย 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก