【多数组合 数学 字符串】2514. 统计同位异构字符串数目

本文涉及知识点

多数组合 数学 字符串

LeetCode2514. 统计同位异构字符串数目

给你一个字符串 s ,它包含一个或者多个单词。单词之间用单个空格 ’ ’ 隔开。
如果字符串 t 中第 i 个单词是 s 中第 i 个单词的一个 排列 ,那么我们称字符串 t 是字符串 s 的同位异构字符串。
比方说,“acb dfe” 是 “abc def” 的同位异构字符串,但是 “def cab” 和 “adc bef” 不是。
请你返回 s 的同位异构字符串的数目,由于答案可能很大,请你将它对 109 + 7 取余 后返回。
示例 1:
输入:s = “too hot”
输出:18
解释:输入字符串的一些同位异构字符串为 “too hot” ,“oot hot” ,“oto toh” ,“too toh” 以及 “too oht” 。
示例 2:
输入:s = “aa”
输出:1
解释:输入字符串只有一个同位异构字符串。
提示:
1 <= s.length <= 105
s 只包含小写英文字母和空格 ’ ’ 。
相邻单词之间由单个空格隔开。

多数组合

每个单词分别求异构词的数量,然后相乘。
求单词的异构词。cnt记录26个字母的出现次数。则异构词的数量:
t o t a l [ x ] = ∑ i : 0 x − 1 c n t [ i ] total[x]=\sum_{i:0}^{x-1}cnt[i] total[x]=i:0x1cnt[i]
异构词的数量 = Π i : 0 26 C t o t a x [ 26 ] − t o t a l [ i ] c n t [ j ] 异构词的数量=\Pi_{i:0}^{26}C_{totax[26]-total[i]}^{cnt[j]} 异构词的数量=Πi:026Ctotax[26]total[i]cnt[j]
就是每个字符的组合相乘。

代码

核心代码

template<int MOD = 1000000007>
class C1097Int
{
public:
	C1097Int(long long llData = 0) :m_iData(llData% MOD)
	{

	}
	C1097Int  operator+(const C1097Int& o)const
	{
		return C1097Int(((long long)m_iData + o.m_iData) % MOD);
	}
	C1097Int& operator+=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData + o.m_iData) % MOD;
		return *this;
	}
	C1097Int& operator-=(const C1097Int& o)
	{
		m_iData = (m_iData + MOD - o.m_iData) % MOD;
		return *this;
	}
	C1097Int  operator-(const C1097Int& o)
	{
		return C1097Int((m_iData + MOD - o.m_iData) % MOD);
	}
	C1097Int  operator*(const C1097Int& o)const
	{
		return((long long)m_iData * o.m_iData) % MOD;
	}
	C1097Int& operator*=(const C1097Int& o)
	{
		m_iData = ((long long)m_iData * o.m_iData) % MOD;
		return *this;
	}
	C1097Int  operator/(const C1097Int& o)const
	{
		return *this * o.PowNegative1();
	}
	C1097Int& operator/=(const C1097Int& o)
	{
		*this /= o.PowNegative1();
		return *this;
	}
	bool operator==(const C1097Int& o)const
	{
		return m_iData == o.m_iData;
	}
	bool operator<(const C1097Int& o)const
	{
		return m_iData < o.m_iData;
	}
	C1097Int pow(long long n)const
	{
		C1097Int iRet = 1, iCur = *this;
		while (n)
		{
			if (n & 1)
			{
				iRet *= iCur;
			}
			iCur *= iCur;
			n >>= 1;
		}
		return iRet;
	}
	C1097Int PowNegative1()const
	{
		return pow(MOD - 2);
	}
	int ToInt()const
	{
		return m_iData;
	}
private:
	int m_iData = 0;;
};

template<class T >
class CFactorial
{
public:
	CFactorial(int n):m_res(n+1){
		m_res[0] = 1;
		for (int i = 1; i <= n; i++) {
			m_res[i] = m_res[i - 1] * i;
		}
	}	
	T Com(int iSel, int iCanSel) {
		return m_res[iCanSel] / m_res[iSel]/ m_res[iCanSel - iSel];
	}
	T Com(const vector<int>& cnt) {
		T biRet = 1;
		int iCanSel = std::accumulate(cnt.begin(), cnt.end(), 0);
		for (int j = 0; j < cnt.size(); j++) {
			biRet *= Com(cnt[j], iCanSel);
			iCanSel -= cnt[j];
		}
		return biRet;
	}
	vector<T> m_res;
};

class Solution {
public:
	int countAnagrams(string s) {
		CFactorial<C1097Int<>> fac(s.length());
		vector<int> cnt(26);
		s += ' ';
		C1097Int<> biRet = 1;
		for (const auto& ch : s) {
			if (' ' == ch) {
				biRet *= fac.Com(cnt);
				cnt.assign(26, 0);
			}
			else {
				cnt[ch - 'a']++;
			}
		}
		return biRet.ToInt();
	}
};

代码

template<class T>
void Assert(const T& t1, const T& t2)
{
    assert(t1 == t2);
}

template<class T>
void Assert(const vector<T>& v1, const vector<T>& v2)
{
    if (v1.size() != v2.size())
    {
        assert(false);
        return;
    }
    for (int i = 0; i < v1.size(); i++)
    {
        Assert(v1[i], v2[i]);
    }

}

int main()
{
	string s;
	{
		Solution sln;
		s = "too hot";
		auto res = sln.countAnagrams(s);
		Assert(18, res);
	}
	{
		Solution sln;
		s = "aa";
		auto res = sln.countAnagrams(s);
		Assert(1, res);
	}
	
}

扩展阅读

视频课程

有效学习:明确的目标 及时的反馈 拉伸区(难度合适),可以先学简单的课程,请移步CSDN学院,听白银讲师(也就是鄙人)的讲解。
https://edu.csdn.net/course/detail/38771

如何你想快速形成战斗了,为老板分忧,请学习C#入职培训、C++入职培训等课程
https://edu.csdn.net/lecturer/6176

相关下载

想高屋建瓴的学习算法,请下载《喜缺全书算法册》doc版
https://download.csdn.net/download/he_zhidan/88348653

我想对大家说的话
闻缺陷则喜是一个美好的愿望,早发现问题,早修改问题,给老板节约钱。
子墨子言之:事无终始,无务多业。也就是我们常说的专业的人做专业的事。
如果程序是一条龙,那算法就是他的是睛

测试环境

操作系统:win7 开发环境: VS2019 C++17
或者 操作系统:win10 开发环境: VS2022 C++17
如无特殊说明,本算法用**C++**实现。

  • 21
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闻缺陷则喜何志丹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值