समस्या
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.
सीमाएँ
0 ≤ length of text ≤ 10,000
उदाहरण
इनपुट:
text = "programming"
अपेक्षित आउटपुट:
expected = "pormig"
इनपुट:
std::string text = "programming";
अपेक्षित आउटपुट:
std::string expected = "pormig";
इनपुट:
string text = "programming";
अपेक्षित आउटपुट:
string expected = "pormig";
व्याख्या
Positions 0, 2, 4, 6, 8, 10 give p, o, r, m, i, g: "pormig".
इनपुट:
text = "a"
अपेक्षित आउटपुट:
expected = "a"
इनपुट:
std::string text = "a";
अपेक्षित आउटपुट:
std::string expected = "a";
इनपुट:
string text = "a";
अपेक्षित आउटपुट:
string expected = "a";
व्याख्या
Only position 0 exists: "a".
कैसे हल करें
पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।
संकेत 1
Tumhein positions 0, 2, 4, aur aise hi characters chahiye.
संकेत 2
Step-by-two traversal sabse simple tareeka hai unhe select karne ka.
संकेत 3
Agar text empty ho ya odd length ho, pattern phir bhi naturally kaam karega.
अपेक्षित जटिलता
समय O(n), अतिरिक्त स्थान O(n)
संदर्भ
यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।