Java Anagrams

  • + 0 comments

    i was done it , without using Arrays.sort() ---> but Time Compelxity is O(n^2) in worst case scenario :(

    char ch1[] = a.toLowerCase().toCharArray(); char ch2[] = b.toLowerCase().toCharArray();

        for(int c = 0; c < ch1.length-1; c++){
            for(int cc = 0; cc < ch1.length-c-1; cc++){
                if(ch1[cc] > ch1[cc+1]){
                    //swap
                    char temp = ch1[cc];
                    ch1[cc] = ch1[cc+1];
                    ch1[cc+1] = temp;
    
                }
            }
        }
    
        String ch1_str = new String(ch1);
    
        // 
        for(int c = 0; c < ch2.length-1; c++){
            for(int cc = 0; cc < ch2.length-c-1; cc++){
                if(ch2[cc] > ch2[cc+1]){
                    //swap
                    char temp = ch2[cc];
                    ch2[cc] = ch2[cc+1];
                    ch2[cc+1] = temp;
    
                }
            }
        }
    
        String ch2_str = new String(ch2);
    
    
       if(ch1_str.equals(ch2_str)){
        return true;
       }
    
       return false;
    
    }