Java Solution beats 96% of the runtimes


  • 0
    E
    public class Solution {
        public String frequencySort(String str) {
        	int arr[]=new int[256];
        	for(int i=0;i<str.length();i++){
        		arr[str.charAt(i)]+=1;
        	}
        	StringBuilder sb=new StringBuilder();
        	for(int i=0;i<arr.length;i++){
        		int max=Integer.MIN_VALUE,letter=0,freq=0;
        		for(int j=0;j<arr.length;j++){
        			if(max<arr[j]){
        				letter=j;
        				freq=arr[j];
        				max=freq;
        			}
        		}
        		arr[letter]=0;
        		max=Integer.MIN_VALUE;
        		for(int k=0;k<freq;k++){
        			sb.append((char)letter);
        		}
        	}
        	return sb.toString();
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.