Special array reversal | Practice | GeeksforGeeks

Given a string S, containing special characters and all the alphabets, reverse the string without
affecting the positions of the special characters.
Example 1:
Input: S = "A&B"
Output: "B&A"
Explanation: As we ignore '&' and
then reverse, so answer is "B&A".

â€‹Example 2:
Input: S = "A&x#
Output: "x&A#"
Explanation: we swap only A and x.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function reverse() which takes the string as inputs and returns required reverse string.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Constraints:
1 ≤ |S| ≤ 105
0
sachinupreti1901 day ago
Easiest question ever seen
 
class Solution{   public:   string reverse(string str)   {        //code here.       // halwa;       string ptr="";       int i;       for(i=0;i<str.size();i++)       {           if((str[i]>='A' && str[i]<='Z' )|| (str[i]>='a' && str[i]<='z'))           ptr+=str[i];       }       int j=ptr.size()-1;              string fin="";       for(i=0;i<str.size();i++)       {           if((str[i]>='A' && str[i]<='Z' )|| (str[i]>='a' && str[i]<='z'))           {               fin+=ptr[j];               j--;           }           else           fin+=str[i];       }       return fin;   } };
0
devashishsharma2602 weeks ago
JAVA SOLUTION
 
class Solution{   public String reverse(String str)   {       //complete the function here       char arr[]=str.toCharArray();       Stack<Character>st=new Stack<>();              for(int i=0;i<arr.length;i++){           if(arr[i]>='a'&&arr[i]<='z'||arr[i]>='A'&&arr[i]<='Z')           st.push(arr[i]);       }       for(int i=0;i<arr.length;i++){           if(arr[i]>='a'&&arr[i]<='z'||arr[i]>='A'&&arr[i]<='Z')           arr[i]=st.pop();       }        String res = String.copyValueOf(arr);        return res;   }}
 
+1
mayank20212 months ago
C++string reverse(string str)   {              string rev="";      for(int i=str.length()-1; i>=0; i--)       {           if( (str[i]>='a' &&str[i]<='z') || (str[i]>='A' &&str[i]<='Z') )              rev=rev+str[i];                              }      // cout<<"--1--"<<rev<<endl;      int j=0;      for(int i=0; i<=str.length()-1; i++)       {           if( (str[i]>='a' &&str[i]<='z') || (str[i]>='A' &&str[i]<='Z') )              {              str[i]=rev[j];              j++;              }                              }              return str;               }
0
sharma_nitin_262 months ago
class Solution
{
    public:
    string reverse(string str)
    { 
        int i=0;
        int j=str.size()-1;
        
        while(i<j){
            while(i<j){
                if(str[i]>='a' && str[i]<='z' || str[i]>='A' && str[i]<='Z')
                    break;
                i++;
            }
            while(i<j){
                if(str[j]>='a' && str[j]<='z' || str[j]>='A' && str[j]<='Z')
                    break;
                j--;
            }
            swap(str[i],str[j]);
            i++;
            j--;
        }
        return str;
    } 
};

+1
ayushjha520013 months ago
//Java Solution 
class Solution{   public String reverse(String s)   {       //complete the function here       char[] str=s.toCharArray();       char a;       for(int i=0,j=s.length()-1;i<s.length() && i<=j;)       {       if(!Character.isAlphabetic(str[i]))       {       i++;           }       else if(!Character.isAlphabetic(str[j]))       {       j--;           }       else       {       a=str[i];       str[i]=str[j];       str[j]=a;       i++;       j--;       }       }   String ans=String.valueOf(str);       return ans;   }}
0
ilihaspatel443 months ago
// simple c++ solution     
string reverse(string str)   {        int i=0;       int j=str.size()-1;       while(i<j){           while(isalpha(str[i]) and isalpha(str[j]) and i<j){               swap(str[i],str[j]);               i++;               j--;           }           while(!isalpha(str[i]) and i<j ){               i++;           }            while(!isalpha(str[j]) and j>i ){           j--;           }                             }       return str;   } 
+1
illiyazzr3 months ago
C++ Solution :-
 
bool alphabet(char ch){   if(ch>=65 && ch<=90 || ch>=97 && ch<=122){       return true;   }   return false;}void swap(int *x,int *y){   int temp = *x;   *x=*y;   *y=temp;}int main(){  string str = "A&x#";  int i=0,j=str.size()-1;  while(i<j){      if(alphabet(str[i])&&alphabet(str[j])){          swap(str[i++],str[j--]);      }      else if(alphabet(str[i])){j--;}      else if(alphabet(str[j])){i++;}      else{i++;j--;}  }  cout<<str;}
0
amiransarimy3 months ago
Python Solutions
 
def reverse(self, s):
       
       i = 0 # left most index
       j = len(s)-1 # right most index
       mlist = list(s) #convert string into list
          
       while i <= j and j >=0:
          if not mlist[i]. isalpha():
              i += 1
          
          elif not mlist[j].isalpha():
              j -= 1
              
          else:
              mlist[i], mlist[j] = mlist[j],mlist[i]
              i += 1 # increment one index from left
              j -= 1  # decrement one index from right
       
       return ''.join(mlist)
0
yashpatil710973 months ago
int i=0;       int j=s1.length()-1;       char []arr1=s1.toCharArray();       while(i<j)       {           char ch1=arr1[i];           char ch2=arr1[j];           if(((ch1>=97&&ch1<=122)||(ch1>=65&&ch1<=90))==false)           {               i++;               continue;           }           else if(((ch2>=97&&ch2<=122)||(ch2>=65&&ch2<=90))==false)           {               j--;               continue;           }           else           {               char temp=arr1[i];               arr1[i]=arr1[j];               arr1[j]=temp;               i++;               j--;           }       }       String res=String.valueOf(arr1);       return res;
+3
badgujarsachin836 months ago
string reverse(string str)
    { 
        //code here.
        int low=0;
        int high=str.size()-1;
        while(low<=high){
            if(!((str[low]>='a'&& str[low]<='z') || (str[low]>='A'&& str[low]<='Z'))){
                low++;
                continue;
            }else if(!((str[high]>='a'&& str[high]<='z') || (str[high]>='A'&& str[high]<='Z'))){
                
                high--;
                continue;
            }
            swap(str[low],str[high]);
            low++;high--;
        }
        return str;
        
    } 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.