CTCI 1.3

Description: Given two strings, write a method to decide if one is a permutation of the other.

We could use the same idea from CTCI 1.1. The only difference is that in 1.1 we just need to know whether one character appear or not, in this problem we need to count its apperance. Since we could use additional data structure, I adopt HashMap to implement the algorithm. If the string is ASCII, we could use array instead, too. Firstly, if the two string's length does not match, return false, else iterate the first string, record each character and its number of apperance and then iterate the other string, if a given character is not contained, return false, else minus the corresponding number, if the number reduces to 0, remove the entry from the HashMap. At last, return true.

/* If the length of two string does not equal, return false;
    Else use a HashMap to count the character and its corresponding number
    Iterate the other string, if one character does not appear, return false;
    Else just minus the numbre by one
    if the number is 0, remove the corresponding entery
    At last, check if the HashMap is null, if not, return false;
    Else return true; 
*/
import java.util.*;

public class IsPermutation {
    public boolean isPermutation(String s1, String s2) {
        if(s1.length() != s2.length()) return false;
        HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        for(int i = 0; i < s1.length(); i++) {
            char ch = s1.charAt(i);
            if(map.containsKey(ch) == false) {
                map.put(ch, 1);
            }
            else {
                map.put(ch, map.get(ch)+1);
            }
        }

        for(int i = 0; i < s2.length(); i++) {
            char ch = s2.charAt(i);
            if(map.containsKey(ch) == false) return false;
            else {
                map.put(ch, map.get(ch)-1);
                if(map.get(ch) == 0) map.remove(ch);
            }
        }
        //if(map.size() != 0) return false;
        return true;
    }

    public static void main(String[] args) {
        IsPermutation ip = new IsPermutation();
        System.out.println(ip.isPermutation("", ""));
        System.out.println(ip.isPermutation("aa", "a"));
        System.out.println(ip.isPermutation("babaa", "bbaaa"));
        System.out.println(ip.isPermutation("abc", "abd"));
    }
}

The time complexity and space complexity both are O(N). If we are allowed to destroy the original strings, we could sort the string, and compare each character one by one. This could use no extra space determined by the sort algorithm chosen but the time complexity would be O(NlogN).

 

转载于:https://www.cnblogs.com/pyemma/p/3826224.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值