Given a positive integer N representing a string of length N with only the letter ‘ z ,’ the task is to find the Lexicographically largest string by merging adjacent identical letters into a single letters of previous alphabet (e.g. Merging “zz” would become “y” and Merging “yy” would become “x” and so on).
Note: “aa” cannot be further mergered.
Examples :
Input : N = 10 Output : db Explanation : The original string “aaaaaaaaaa” can be transformed as follows.
“zzzzzzzzzz”
“yyyyy”
“xxy”
“wy”
Input : N = 2 Output : y
Approach:
Find the largest power of 2 in N , because the largest power of 2 represents the maximum number of consecutive ‘z ‘s that can be transformed into the previous letter, then construct the resulting string and then convert the resulting value into the corresponding alphabet.
Start with the first letter ‘ z ‘ and Subtract the value of the largest power of 2 from N , repeat the process until N becomes 0 and then finally convert the resulting value into the corresponding alphabet.
Steps-by-step approach:
Finds the largest power of 2 in the given number n (capped at 25).
Returns a pair with the exponent and the value of the largest power of 2.
Constructs a string by repeatedly finding and appending characters based on the largest power of 2 in n.
Using the largestPowerof2() function to find and append the corresponding character and Continue the process until n becomes zero.
Return the constructed string.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
pair<ll, ll> largestPowerof2(ll n)
{
ll ans = 0;
for (ll i = 30; i >= 0; i--) {
if ((1LL << i) & n) {
ans = i;
break;
}
}
ans = min(25LL, ans);
return { ans, 1LL << ans };
}
string constructString(ll n)
{
string str = "zyxwvutsrqponmlkjihgfedcba";
string ans = "";
while (n) {
pair<ll, ll> a = largestPowerof2(n);
ll pw = a.first;
ll value = a.second;
ans += str[pw];
n -= value;
}
return ans;
}
int main()
{
int n = 10;
string result = constructString(n);
cout << result;
return 0;
}
Java
import java.util.*;
public class Main {
static class Pair {
long first;
long second;
Pair(long first, long second) {
this.first = first;
this.second = second;
}
}
static Pair largestPowerof2(long n) {
long ans = 0;
for (long i = 30; i >= 0; i--) {
if (((1L << i) & n) != 0) {
ans = i;
break;
}
}
ans = Math.min(25, ans);
return new Pair(ans, 1L << ans);
}
static String constructString(long n) {
String str = "abcdefghijklmnopqrstuvwxyz";
StringBuilder ans = new StringBuilder();
while (n != 0) {
Pair a = largestPowerof2(n);
long pw = a.first;
long value = a.second;
ans.append(str.charAt((int) pw));
n -= value;
}
return ans.toString();
}
public static void main(String[] args) {
long n = 11;
String result = constructString(n);
System.out.println(result);
}
}
Python3
def largestPowerof2(n):
ans = 0
for i in range(30, -1, -1):
if (1 << i) & n:
ans = i
break
ans = min(25, ans)
return ans, 1 << ans
def constructString(n):
str = "zyxwvutsrqponmlkjihgfedcba"
ans = ""
while n:
pw, value = largestPowerof2(n)
ans += str[pw]
n -= value
return ans
def main():
n = 10
result = constructString(n)
print(result)
if __name__ == "__main__":
main()
Time Complexity : O(log n), the loop runs a constant number of times (dependent on the binary representation of n) Auxiliary Space : O(1), uses a constant amount of space for variables.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
14 Jan, 2024
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...