Checking whether two numbers are permutation of each other?

前端 未结 2 1266
别跟我提以往
别跟我提以往 2021-02-15 23:08

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

2条回答
  •  遇见更好的自我
    2021-02-15 23:45

    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");
    

提交回复
热议问题