forked from dubey-harshit/Hacktoberfest-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigzagconvertion.cpp
More file actions
47 lines (36 loc) · 972 Bytes
/
Zigzagconvertion.cpp
File metadata and controls
47 lines (36 loc) · 972 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution {
public:
string convert(string s, int numRows) {
if (numRows == 1 || numRows >= s.size()) return s;
string ans;
vector<vector<char>> rows(numRows);
int k = 0;
int direction = -1;
for (const char c : s) {
rows[k].push_back(c);
if (k == 0 || k == numRows - 1)
direction *= -1;
k += direction;
}
for (const vector<char>& row : rows)
for (const char c : row)
ans += c;
return ans;
}
};
int main() {
Solution solution;
string input;
int numRows;
cout << "Enter a string: ";
getline(cin, input);
cout << "Enter the number of rows: ";
cin >> numRows;
string result = solution.convert(input, numRows);
cout << "Converted string: " << result << endl;
return 0;
}