Checking whether two numbers are permutation of each other?

前端 未结 7 2019
借酒劲吻你
借酒劲吻你 2021-02-15 22:44

Given two numbers a, b such that 1 <= a , b <= 10000000000 (10^10). My problem is to check whether the digits in them are permutation of each other or not. What is the fas

7条回答
  •  遥遥无期
    2021-02-15 23:36

    a and b are anagrams if they have the same number of each digit. So basically the fastest way seems to be, counting the digits for a and b:

    int c[10]={0,0,0,0,0,0,0,0,0,0}
    
    while (a) { c[a%10]++; a/=10; }
    while (b) { c[b%10]--; b/=10; }
    
    int res=1;
    for (int i=0;i<10;i++) res &= c[i]==0;
    printf(res?"yes":"no");
    

提交回复
热议问题