Accepted solution. Java. O(n+m) space complexity


  • 0
    O
    public class Solution {
        public boolean isAnagram(String s, String t) {
        	if (s.length() > t.length() || s.length() < t.length()) {
        		return false;
        	}
        	if (s.equals(t)) {
        		return true;
        	}
        	char[] sChars = s.toCharArray();
        	char[] tChars = t.toCharArray();
        	Arrays.sort(sChars);
        	Arrays.sort(tChars);
        	for (int i = 0; i < s.length(); i++) {
        		if (sChars[i] != tChars[i]) {
        			return false;
        		}
        	}
            return true;
        }
    }

  • 0
    N

    Which sort is done in O(n) complexity?


  • 0
    O

    Actually counting sort, radix sort is a O(n) time complexity algorithms, but in this case you are right because Arrays.sort it is Quick sort algorithm the worst case of which is O(n^2).
    Thanks for correction))


  • 0
    R

    Those are not comparison based sort


  • 0
    O

    True) But the question was about sort in general.


Log in to reply
 

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