Đề bài
Build a new string from the characters of text at positions 0, 2, 4, … (every other character, starting with the first).
Implement every_other_character(text).
Input
text: The original text.
Output
Return the characters at even positions, in order.
Ràng buộc
0 ≤ length of text ≤ 10,000
Ví dụ
Đầu vào:
text = "programming"
Kết quả mong đợi:
expected = "pormig"
Đầu vào:
std::string text = "programming";
Kết quả mong đợi:
std::string expected = "pormig";
Đầu vào:
string text = "programming";
Kết quả mong đợi:
string expected = "pormig";
Giải thích
Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".
Đầu vào:
text = "a"
Kết quả mong đợi:
expected = "a"
Đầu vào:
std::string text = "a";
Kết quả mong đợi:
std::string expected = "a";
Đầu vào:
string text = "a";
Kết quả mong đợi:
string expected = "a";
Giải thích
Only position 0 exists: "a".
Hướng tiếp cận
Hãy tự thử trước, sau đó mở từng gợi ý một.
Gợi ý 1
You want characters at positions 0, 2, 4, and so on.
Gợi ý 2
A step-by-two traversal is the simplest way to select them.
Gợi ý 3
If the text is empty or has odd length, the pattern still works naturally.
Độ phức tạp mong đợi
O(n) time, O(n) extra space
Tham khảo
Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.