给定一个字符串,找出所需的最小删除次数,这样字符串中就不会有两个连续的重复字符。 例如:
null
Input : AAABBBOutput : 4Explanation : New string should be ABInput : ABABABABOutput : 0Explanation : There are no consecutive repeating characters.
如果有n个连续的相同字符,则从这n个字符中删除n-1个。
C++
// CPP code to count minimum deletions required // so that there are no consecutive characters left #include <bits/stdc++.h> using namespace std; int countDeletions(string str) { int ans = 0; for ( int i = 0; i < str.length() - 1; i++) // If two consecutive characters are // the same, delete one of them. if (str[i] == str[i + 1]) ans++; return ans; } // Driver code int main() { string str = "AAABBB" ; // Function call to print answer cout << countDeletions(str); return 0; } |
JAVA
// Java code to count minimum deletions required // so that there are no consecutive characters left import java.util.*; class GFG { static int countDeletions(String s) { int ans = 0 ; char [] str = s.toCharArray(); for ( int i = 0 ; i < str.length - 1 ; i++) // If two consecutive characters are // the same, delete one of them. if (str[i] == str[i + 1 ]) ans++; return ans; } // Driver code public static void main(String[] args) { String str = "AAABBB" ; // Function call to print answer System.out.println(countDeletions(str)); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python3
# Python code to count minimum deletions required # so that there are no consecutive characters left def countDeletions(string): ans = 0 for i in range ( len (string) - 1 ): # If two consecutive characters are # the same, delete one of them. if (string[i] = = string[i + 1 ]): ans + = 1 return ans # Driver code string = "AAABBB" # Function call to print answer print (countDeletions(string)) # This code is contributed by Sachin Bisht |
C#
// C# Program to count minimum deletions // required so that there are no // consecutive characters left using System; class GFG { // Function for counting deletions static int countDeletions(String s) { int ans = 0; char []str = s.ToCharArray(); for ( int i = 0; i < str.Length - 1; i++) // If two consecutive characters are // the same, delete one of them. if (str[i] == str[i + 1]) ans++; return ans; } // Driver code public static void Main() { String str = "AAABBB" ; // Function call to print answer Console.Write(countDeletions(str)); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP code to count minimum // deletions required so that // there are no consecutive // characters left function countDeletions( $str ) { $ans = 0; for ( $i = 0; $i < strlen ( $str ) - 1; $i ++) // If two consecutive characters are // the same, delete one of them. if ( $str [ $i ] == $str [ $i + 1]) $ans ++; return $ans ; } // Driver Code $str = "AAABBB" ; // Function call to // print answer echo countDeletions( $str ); // This code is contributed by nitin mittal ?> |
Javascript
<script> // javascript code to count minimum deletions required // so that there are no consecutive characters left function countDeletions(s) { var ans = 0; var str = s; for ( var i = 0; i < str.length - 1; i++) // If two consecutive characters are // the same, delete one of them. if (str[i] == str[i + 1]) ans++; return ans; } // Driver code var str = "AAABBB" ; // Function call to print answer document.write(countDeletions(str)); // This code is contributed by gauravrajput1 </script> |
输出:
4
本文由 罗希特·塔普里亚尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END