You are given a license key represented as a string s that consists of only alphanumeric characters and dashes. The string is separated into n + 1 groups by n dashes. You are also given an integer k.

We want to reformat the string s such that each group contains exactly k characters, except for the first group, which could be shorter than k but still must contain at least one character. Furthermore, there must be a dash inserted between two groups, and you should convert all lowercase letters to uppercase.

Return the reformatted license key.

&nbsp;
Example 1:

Input: s = "5F3Z-2e-9-w", k = 4
Output: "5F3Z-2E9W"
Explanation: The string s has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.


Example 2:

Input: s = "2-5g-3-J", k = 2
Output: "2-5G-3J"
Explanation: The string s has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.


&nbsp;
Constraints:


	1 &lt;= s.length &lt;= 105
	s consists of English letters, digits, and dashes '-'.
	1 &lt;= k &lt;= 104


class Solution:
    def licenseKeyFormatting(self, s: str, k: int) -> str:
        new_str = s.replace("-", "")
        res = ""
        j = len(new_str)-1
        i = 0
        while j >= 0:
            res += new_str[j].upper()
            i += 1
            if i == k and j != 0:
                res += "-"
                i = 0
            j -= 1
        return res[::-1]
class Solution {
    public String licenseKeyFormatting(String s, int k) {
        StringBuilder answer = new StringBuilder();
        int length = 0;
        // Iterate Backwards to fullfill first group condition
        for(int i=s.length()-1;i>=0;i--) {
            if(s.charAt(i) == '-') {
                continue;
            }
            if(length > 0 && length % k == 0) {
                answer.append('-');
            }
            answer.append(Character.toUpperCase(s.charAt(i)));
            length++;
        }
        return answer.reverse().toString();
    }
}
class Solution {
public:
    string licenseKeyFormatting(string s, int k) {
        stack<char>st;
        string ans="";
        for(int i=0;i<s.length();i++){
            if(isalpha(s[i]) || isdigit(s[i])){
                st.push(s[i]);
            }
        }
        int i=0;
        while(st.size()>0){
            char ch=st.top();
            st.pop();
            if(isalpha(ch)) 
            {
                ch=toupper(ch);
            }
            ans+=ch;
            if((i+1)%k==0 && st.size()!=0) ans+='-';
            i++;
        }
        reverse(ans.begin(),ans.end());
        return ans;
    }
};
// Please upvote if you like the solution. Thanks

var licenseKeyFormatting = function(s, k) {
 let str=s.replace(/[^A-Za-z0-9]/g,"").toUpperCase()
    let ans=""
    let i=str.length;
    while(i>0){
        ans="-"+str.substring(i-k,i)+ans // we are taking k characters from the end of string and adding it to answer
        i=i-k
    }
    return (ans.substring(1)) // removing the "-" which is present in the start of ans
};
License Key Formatting