给定一个字符串str和一个整数K ,任务是检查它是否由K个交替字符。
例子:
Input: str = “acdeac”, K = 4
Output: Yes
Input: str = “abcdcab”, K = 2
Output: No
方法:为了使字符串由K个交替的字符,它必须满足以下条件:
- (i + mK)索引处的所有字符必须相同,其中i是当前索引, mK代表K的第m倍。这意味着在每K个索引之后,必须重复该字符。
- 相邻字符不能相同。这是因为,如果字符串的类型为“ AAAAA”,并且单个字符重复任意次,则上述条件将被匹配,但是在这种情况下,答案必须为“否”。
下面的代码是上述方法的实现:
C++
// C++ implementation of the approach #include
using namespace std; // Function to check if a string // is made up of k alternating characters bool isKAlternating(string s, int k) { if (s.length() < k) return false; int checker = 0; // Check if all the characters at // indices 0 to K-1 are different for (int i = 0; i < k; i++) { int bitAtIndex = s[i] - 'a'; // If that bit is already set in // checker, return false if ((checker & (1 << bitAtIndex)) > 0) { return false; } // Otherwise update and continue by // setting that bit in the checker checker = checker | (1 << bitAtIndex); } for (int i = k; i < s.length(); i++) if (s[i - k] != s[i]) return false; return true; } // Driver code int main() { string str = "acdeac"; int K = 4; if (isKAlternating(str, K)) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
Java
// Java implementation of the approach class GFG{ // Function to check if a String // is made up of k alternating characters static boolean isKAlternating(String s, int k) { if (s.length() < k) return false; int checker = 0; // Check if all the characters at // indices 0 to K-1 are different for (int i = 0; i < k; i++) { int bitAtIndex = s.charAt(i) - 'a'; // If that bit is already set in // checker, return false if ((checker & (1 << bitAtIndex)) > 0) { return false; } // Otherwise update and continue by // setting that bit in the checker checker = checker | (1 << bitAtIndex); } for (int i = k; i < s.length(); i++) if (s.charAt(i - k) != s.charAt(i) ) return false; return true; } // Driver code public static void main(String[] args) { String str = "acdeac"; int K = 4; if (isKAlternating(str, K)) System.out.print("Yes" +"\n"); else System.out.print("No" +"\n"); } } // This code is contributed by sapnasingh4991
Python3
# Python 3 implementation of the approach # Function to check if a string # is made up of k alternating characters def isKAlternating( s, k): if (len(s) < k): return False checker = 0 # Check if all the characters at # indices 0 to K-1 are different for i in range( k): bitAtIndex = ord(s[i]) - ord('a') # If that bit is already set in # checker, return false if ((checker & (1 << bitAtIndex)) > 0): return False # Otherwise update and continue by # setting that bit in the checker checker = checker | (1 << bitAtIndex) for i in range(k,len(s)): if (s[i - k] != s[i]): return False return True # Driver code if __name__ =="__main__": st = "acdeac" K = 4 if (isKAlternating(st, K)): print ("Yes") else: print ("No") # This code is contributed by chitranayal
C#
// C# implementation of the approach using System; class GFG{ // Function to check if a String // is made up of k alternating characters static bool isKAlternating(String s, int k) { if (s.Length < k) return false; int checker = 0; // Check if all the characters at // indices 0 to K-1 are different for (int i = 0; i < k; i++) { int bitAtIndex = s[i] - 'a'; // If that bit is already set in // checker, return false if ((checker & (1 << bitAtIndex)) > 0) { return false; } // Otherwise update and continue by // setting that bit in the checker checker = checker | (1 << bitAtIndex); } for (int i = k; i < s.Length; i++) if (s[i - k] != s[i] ) return false; return true; } // Driver code public static void Main() { String str = "acdeac"; int K = 4; if (isKAlternating(str, K)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This article contributed by AbhiThakur
输出:Yes
时间复杂度: O(N)