You are given an alphanumeric string s. (Alphanumeric string is a string consisting of lowercase English letters and digits).

You have to find a permutation of the string where no letter is followed by another letter and no digit is followed by another digit. That is, no two adjacent characters have the same type.

Return the reformatted string or return an empty string if it is impossible to reformat the string.

&nbsp;
Example 1:

Input: s = "a0b1c2"
Output: "0a1b2c"
Explanation: No two adjacent characters have the same type in "0a1b2c". "a0b1c2", "0a1b2c", "0c2a1b" are also valid permutations.


Example 2:

Input: s = "leetcode"
Output: ""
Explanation: "leetcode" has only characters so we cannot separate them by digits.


Example 3:

Input: s = "1229857369"
Output: ""
Explanation: "1229857369" has only digits so we cannot separate them by characters.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 500
	s consists of only lowercase English letters and/or digits.


class Solution:
    def reformat(self, s: str) -> str:
        # Store the alphabets and the numerics from the string in a seperat arrays
        alpha = []
        num = []
        # Initiate a res variable to store the resultant string
        res = ''
        
        for i in s:
            if i.isalpha():
                alpha.append(i)
            else:
                num.append(i)
                
        # It's not possible to create a permutation if the absolute difference b/w len(alpha) and len(num) > 1.
        if abs(len(alpha)-len(num)) > 1: return ''
        
        # Use Zip to create list of tuples.
        # For ex:- if alpha = ['a','b'] and num = ['1', '2'] then,
        # zip(alpha, num) = [('a', '1'), ('b', '2')]
        for ch, n in zip(alpha, num):
            res += (ch+n)
            
        if len(alpha) > len(num):
            res += alpha[-1]
        if len(num) > len(alpha):
            res = num[-1] + res
            
        return res
class Solution {
    public String reformat(String s) {
        
        List<Character> ch = new ArrayList<>();
        List<Character> d = new ArrayList<>();
        
        for(char c : s.toCharArray()){
            if(c >= 'a' && c <= 'z')ch.add(c);
            else d.add(c);
        }
        
        if(Math.abs(d.size() - ch.size()) > 1)  return "";
        
        StringBuilder str = new StringBuilder();
        
        for(int i = 0; i < s.length(); i++){
            
            if(!ch.isEmpty() || !d.isEmpty()){
                if(ch.size() > d.size())
                    str.append(appender(ch,d));
                else 
                    str.append(appender(d,ch));
            }
            else{
                break;
            }
        }
        
        return new String(str);
        
    }
    
    public String appender(List<Character> first,List<Character> second){
        
        StringBuilder str = new StringBuilder();
        
        if(!first.isEmpty()){
            str.append(first.get(0));
            first.remove(0);
        }
        if(!second.isEmpty()){
            str.append(second.get(0));
            second.remove(0);
        }
        
        return new String(str);
    }
}
class Solution {
public:
    string reformat(string s) {
        string dg,al;
        for(auto&i:s)isdigit(i)?dg+=i:al+=i;
        if(abs((int)size(dg)-(int)size(al))>1) return "";
        int i=0,j=0,k=0;
        string ans(size(s),' ');
        bool cdg=size(dg)>size(al);
        while(k<size(s)){
            if(cdg)ans[k++]=dg[i++];
            else ans[k++]=al[j++];
            cdg=!cdg;
        }
        return ans;
    }
};
var reformat = function(s) {
    let letter=[], digit=[];
    for(let i=0; i<s.length; i++){
        s[i]>=0 && s[i]<=9? digit.push(s[i]): letter.push(s[i]);
    }
	// impossible to reformat
    if(Math.abs(letter.length-digit.length)>=2){return ""}
    
    let i=0, output="";
    while(i<letter.length && i<digit.length){
        output=output+letter[i]+digit[i]; i++;
    }
    if(i<letter.length){output=output+letter[i]}; // add in the END
    if(i<digit.length){output=digit[i]+output}; // add in the FRONT
    return output;
};
Reformat The String