第 i 个字母是给定单词的第 (i-1)-th、i-th 或 (i+1)-th 个字母的单词计数
给定一个字符串str 。任务是计算与 str 长度相同的单词,并且第 i 个位置的每个字母是 str 的第 (i-1)-th、i-th 或 (i+1)-th 位置字母。
注意:对于第一个字母,考虑 W 的第 i 个和第 (i+1) 个位置字母。对于最后一个字母,考虑 str 的第 (i-1) 个和第 i 个位置字母。
例子:
Input : str[] = "ab"
Output : 4
Words that can be formed: aa, ab, ba, bb.
Input : str[] = "x"
Output : 1
对于索引 i 处的任何字母,除了第一个和最后一个字母外,存在三个可能的字母,即给定单词的第 (i-1) 个、第 i 个或第 (i+1) 个字母。因此,如果其中三个不同,我们就有 3 种可能性。如果其中两个相同,我们有两种可能性。如果一切都一样,我们只有一种可能性。
因此,遍历给定的单词并找到每个字母的可能性并将它们相乘。
同样,对于第一个字母,检查第一个和第二个位置的不同字母。对于最后一个位置,检查最后一个和倒数第二个位置的不同字母。
下面是这种方法的实现:
C++
// C++ program to count words whose ith letter
// is either (i-1)th, ith, or (i+1)th letter
// of given word.
#include
using namespace std;
// Return the count of words.
int countWords(char str[], int len)
{
int count = 1;
// If word contain single letter, return 1.
if (len == 1)
return count;
// Checking for first letter.
if (str[0] == str[1])
count *= 1;
else
count *= 2;
// Traversing the string and multiplying
// for combinations.
for (int j=1; j
Java
// Java program to count words whose ith letter
// is either (i-1)th, ith, or (i+1)th letter
// of given word.
public class GFG {
// Return the count of words.
static int countWords(String str, int len)
{
int count = 1;
// If word contain single letter, return 1.
if (len == 1)
return count;
// Checking for first letter.
if (str.charAt(0) == str.charAt(1))
count *= 1;
else
count *= 2;
// Traversing the string and multiplying
// for combinations.
for (int j = 1; j < len - 1; j++)
{
// If all three letters are same.
if (str.charAt(j) == str.charAt(j - 1) &&
str.charAt(j) == str.charAt(j + 1))
count *= 1;
// If two letter are distinct.
else if (str.charAt(j) == str.charAt(j - 1)||
str.charAt(j) == str.charAt(j + 1) ||
str.charAt(j - 1) == str.charAt(j + 1))
count *= 2;
// If all three letter are distinct.
else
count *= 3;
}
// Checking for last letter.
if (str.charAt(len - 1) == str.charAt(len - 2))
count *= 1;
else
count *= 2;
return count;
}
// Driven Program
public static void main(String args[])
{
String str = "abc";
int len = str.length();
System.out.println(countWords(str, len));
}
}
// This code is contributed by Sumit Ghosh
Python 3
# Python 3 program to count words whose ith letter
# is either (i-1)th, ith, or (i+1)th letter
# of given word.
# Return the count of words.
def countWords( str, l):
count = 1;
# If word contain single letter, return 1.
if (l == 1):
return count
# Checking for first letter.
if (str[0] == str[1]):
count *= 1
else:
count *= 2
# Traversing the string and multiplying
# for combinations.
for j in range(1,l-1):
# If all three letters are same.
if (str[j] == str[j-1] and str[j] == str[j+1]):
count *= 1
# If two letter are distinct.
else if (str[j] == str[j-1] or
str[j] == str[j+1] or
str[j-1] == str[j+1]):
count *= 2
# If all three letter are distinct.
else:
count *= 3
# Checking for last letter.
if (str[l - 1] == str[l - 2]):
count *= 1
else:
count *= 2
return count
# Driven Program
if __name__ == "__main__":
str = "abc"
l = len(str)
print(countWords(str, l))
C#
// C# program to count words whose
// ith letter is either (i-1)th,
// ith, or (i+1)th letter of the
// given word
using System;
public class GFG {
// Return the count of words.
static int countWords(string str, int len)
{
int count = 1;
// If word contain single letter,
// return 1.
if (len == 1)
return count;
// Checking for first letter.
if (str[0] == str[1])
count *= 1;
else
count *= 2;
// Traversing the string and
// multiplying for combinations.
for (int j = 1; j < len - 1; j++) {
// If all three letters are same.
if (str[j] == str[j - 1] &&
str[j] == str[j + 1])
count *= 1;
// If two letter are distinct.
else if (str[j] == str[j - 1] ||
str[j] == str[j + 1] ||
str[j - 1] == str[j + 1])
count *= 2;
// If all three letter are distinct.
else
count *= 3;
}
// Checking for last letter.
if (str[len - 1] == str[len - 2])
count *= 1;
else
count *= 2;
return count;
}
// Driver Program
public static void Main()
{
string str = "abc";
int len = str.Length;
Console.WriteLine(countWords(str, len));
}
}
// This code is contributed by Anant Agarwal
PHP
Javascript
输出:
12
时间复杂度: O(字符串长度)。