若是兄弟,则str1和str2必须最少遍历一遍;若不是兄弟,则可以提前终止遍历。

int func(char *str1, char *str2)
{
	// ASCII nums
	int i, j, count[128];
	char *p1, *p2;
	p1 = str1;
	p2 = str2;
	j = 0;
	// initial array
	for(i=0; i<128; i++)
		count[i] = 0;
	while('\0' != *p1)
	{
		count[*p1++]++;
		j++;
	}
	while('\0' != *p2)
	{
		count[*p2]--;
		j--;
		if(j < 0 || count[*p2] < 0)
			return 0;
		p2++;
	}
	// check 0
	for(i=0; i<128; i++)
	{
		if(count[i] != 0)
			return 0;
	}
	return 1;
}

  


相关文章:

  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-12-03
  • 2021-09-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-02
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2021-09-10
相关资源
相似解决方案