problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::pattern_matching_lcci::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static bool patternMatching (const string &pattern, const string &value)
 

详细描述

在文件 leetcode.h770 行定义.

成员函数说明

◆ patternMatching()

bool leetcode::pattern_matching_lcci::Solution::patternMatching ( const string &  pattern,
const string &  value 
)
static

在文件 leetcode.cpp1783 行定义.

1783 {
1784 int a_count = 0;
1785 int b_count = 0;
1786 for(const char ch: pattern) {
1787 if(ch == 'a') {
1788 a_count++;
1789 } else {
1790 b_count++;
1791 }
1792 }
1793 if(a_count == 0 || b_count == 0) {
1794 int size = 0;
1795 int count = 0;
1796 if(b_count == 0) {
1797 if(value.length() % a_count != 0) {
1798 return false;
1799 }
1800 size = value.length() / a_count;
1801 count = a_count;
1802 } else {
1803 if(value.length() % b_count != 0) {
1804 return false;
1805 }
1806 size = value.length() / b_count;
1807 count = b_count;
1808 }
1809 const string str = value.substr(0, size);
1810 for(int i = 0; i < count; i++) {
1811 auto s = value.substr(i * size, size);
1812 if(s != str) {
1813 return false;
1814 }
1815 }
1816 return true;
1817 }
1818 for(int a_size = 0; a_size <= value.length() / a_count; a_size++) {
1819 string a;
1820 string b;
1821 if((value.length() - a_size * a_count) % b_count == 0) {
1822 const int b_size = (value.length() - a_size * a_count) / b_count;
1823 string value_local = value;
1824 for(const char ch: pattern) {
1825 if(ch == 'a') {
1826 string a_local = value_local.substr(0, a_size);
1827 if(a.empty()) {
1828 a = a_local;
1829 } else if(a_local != a) {
1830 goto next_loop;
1831 }
1832 value_local = value_local.substr(a_size);
1833 } else {
1834 string b_local = value_local.substr(0, b_size);
1835 if(b.empty()) {
1836 b = b_local;
1837 } else if(b_local != b) {
1838 goto next_loop;
1839 }
1840 value_local = value_local.substr(b_size);
1841 }
1842 }
1843 if(a == b) {
1844 goto next_loop;
1845 }
1846 return true;
1847 }
1848 next_loop:;
1849 }
1850 return false;
1851 }

被这些函数引用 leetcode::pattern_matching_lcci::TEST().


该类的文档由以下文件生成: