Thursday, December 3, 2015

Valid Anagram

Q:
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
A:
 Using the fact that they are only lowercase alphabets

public class Solution {
    public boolean isAnagram(String s, String t) {
        int[] A = new int[26];
        int[] B = new int[26];
        for(int i =0;i<s.length();i++)
            A[s.charAt(i)-'a']++;
        for(int i =0;i<t.length();i++)
            B[t.charAt(i)-'a']++;
        for(int i =0;i<26;i++)
            if(A[i]!=B[i])
                return false;
        return true;
    }
}




Mistakes:













1 comment:

  1. There is shocking news in the sports betting world.

    It has been said that any bettor must watch this,

    Watch this or quit placing bets on sports...

    Sports Cash System - Automated Sports Betting Software.

    ReplyDelete