Untitled
unknown
java
a year ago
1.4 kB
101
Indexable
public class GreedyAlgo { public static String smallestString(int n, int k){ StringBuilder sb = new StringBuilder(); int total_chars = n; int csum = k; while(total_chars > 0){ // is it possible to fill 'a' here if((total_chars - 1)*26 >= (csum-1)){ sb.append('a'); csum--; } else { int position = csum % 26; if(position == 0){ sb.append('z'); csum-=26; } else { sb.append((char)(position - 1 +'a')); csum-= position; } } total_chars --; } return sb.toString(); } public static int maxBalancedStrings(String str){ int n = str.length(); int lcount = 0; int rcount = 0; int ans = 0; for(int i=0; i<n; i++){ char ch = str.charAt(i); if(ch == 'L'){ lcount++; } else { rcount++; } if(lcount == rcount){ ans++; // lcount = 0; // rcount = 0; } } return ans; } public static void main(String[] args) { int n = 5; int k = 101; System.out.println(smallestString(n,k)); } }
Editor is loading...
Leave a Comment