Magical String[Duplicate Problem] | Practice | GeeksforGeeks

You are given a string S, convert it into a magical string.
A string can be made into a magical string if the alphabets are swapped in the given manner: a->z or z->a, b->y or y->b, and so on.  
 
Note: All the alphabets in the string are in lowercase.
 
Example 1:
Input:
S = varun
Output:
ezifm
Explanation:
Magical string of "varun" 
will be "ezifm" 
since v->e , a->z , 
r->i , u->f and n->m.

 
Example 2:
Input:
S = akshay
Output:
zphszb
Explanation:
Magical string of "akshay" 
will be "zphszb" 
since a->z , k->p , s->h , 
h->s , a->z and y->b.

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function magicalString() which takes the string S and returns the magical string.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
 
Constraints:
1<=Length of String<=100
 
0
shirsathsantoshi901 week ago
string magicalString(string S){       string s="";       for(int i=0;i<S.length();i++)       {           s+=('z'-S[i]+'a');       }       return s;   }
0
prishita2 weeks ago
Java Solution:
class Solution{
    static String magicalString(String S){
        // code here
        String a="abcdefghijklmnopqrstuvwxyz";
        char[] ch=S.toCharArray();
        int ind;
        for(int i=0;i<ch.length;i++){
            ind=25-a.indexOf(ch[i]);
            ch[i]=a.charAt(ind);
        }
        return String.valueOf(ch);
    }
}
0
imabheek1 month ago
/*JAVA*/ String s = "";       for(int i = 0; i < S.length() ; i++)           {s += (char)(219 - (int)(S.charAt(i)));}       return s;
0
imranmbhd24121 month ago
 
string magicalString(string s){       for(int i = 0; i < s.size(); i++) {           if(s[i] >= 'a' && s[i] <= 'z') {               s[i] =  'z' - (s[i]-'a');           } else if (s[i] >= 'A' && s[i] <= 'Z') {               s[i] =  'Z' - (s[i]-'A');           } else {               continue;              }       }       return s;   }
+1
imohdalam1 month ago
Java | O(n)
class Solution{
    static String magicalString(String S){
        
        StringBuilder ans = new StringBuilder();
        
        for(int i=0; i<S.length(); i++)
              ans.append((char)('z' + 'a' - S.charAt(i)));
        
        return ans.toString();
    }
}
0
rahulraj943912 months ago
Java solution using StringBuilder and Math.abs()
 
static String magicalString(String S) {
       StringBuilder sb = new StringBuilder("");
       for (char cc : S.toCharArray())
           sb.append((char) (122 - Math.abs(97 - cc)));
       return sb.toString();
   }
0
rahulraj943912 months ago
Java solution using Math.abs()
 
static String magicalString(String S) {
       String temp = "";
       for (char cc : S.toCharArray())
           temp += (char) (122 - Math.abs(97 - cc));
       return temp;
   }
0
mridulbhaskarabc2 months ago
#Contributed By: Mridul Bhaskar
class Solution:
    def magicalString (ob,S):
        # code here 
        list_= list(S)
        for i in range(len(list_)):
            list_[i] = 122-ord(list_[i])+97
            list_[i] = chr(list_[i])
            
        list_ = "".join(str(i) for i in list_)
        return list_
 
0
vikasnayakmmmut2 months ago
int n=S.length();       for(int i=0;i<n;i++)       {           if(S[i]<='m')           {               S[i]='a'+(25-(S[i]-'a'));           }           else           {               S[i]='z'-(25-('z'-S[i]));           }       }       return S;
0
madhukartemba2 months ago
SIMPLE JAVA SOLUTION:
class Solution{
    static String magicalString(String S){
        int n = S.length();
        
        String ans = "";
        
        for(int i=0; i<n; i++)
        {
            ans += (char)('a'+'a'+25-S.charAt(i));
        }
        
        return ans;
    }
}
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.