Skip to content

Added ZigZag Conversion problem #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions String/ZigZagConversion/ZigZag_Conversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string convert(string s, int numRows) {
vector<string>v(numRows,"");// for storing the new pattern
int fg=0;//flag for direction
int n=s.size();
for(int i=0;i<n;i++){
if(fg==0){
int count=0;
while(i<n&&count<numRows){
v[count]+=s[i];
i++;
count++;
}
}
else{
int count=numRows-2;
while(i<n && count>0){
v[count]+=s[i];
i++;
count--;
}
}
fg=1-fg;
i--;
}
//forming the final string
string ans="";
for(int i=0;i<numRows;i++){
ans+=v[i];
}
return ans;
}
int main(){
string inputstr;
int numRows;
int testcases;
cin>>testcases;
while(testcases--){
cin>>inputstr;
cin>>numRows;
string a=convert(inputstr,numRows);
cout<<a<<"\n";
}
return 0;
}
48 changes: 48 additions & 0 deletions String/ZigZagConversion/ZigZag_Conversion.cpp~
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include<iostream>
#include<vector>
#include<string>
using namespace std;
string convert(string s, int numRows) {
vector<string>v(numRows,"");// for storing the new pattern
int fg=0;//flag for direction
int n=s.size();
for(int i=0;i<n;i++){
if(fg==0){
int count=0;
while(i<n&&count<numRows){
v[count]+=s[i];
i++;
count++;
}
}
else{
int count=numRows-2;
while(i<n && count>0){
v[count]+=s[i];
i++;
count--;
}
}
fg=1-fg;
i--;
}
//forming the final string
string ans="";
for(int i=0;i<numRows;i++){
ans+=v[i];
}
return ans;
}
int main(){
string inputstr;
int numRows;
int testcases;
cin>>testcases;
while(testcases--){
cin>>inputstr;
cin>>numRows;
string a=convert(inputstr,numRows);
cout<<a<<"\n";
}
return 0;
}
11 changes: 11 additions & 0 deletions String/ZigZagConversion/ZigZag_Conversion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P.......A.......H.......N
..A...P...L...S...I...I...G
....Y.......I.......R

And then read line by line: PAHNAPLSIIGYIR

Write the code that will take a string and make this conversion given a number of rows:
string convert(string text, int nRows);
Convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR"