發表文章

目前顯示的是有「String Manipulation」標籤的文章

String Manipulation - Sherlock and the Valid String [M]

Sherlock considers a string to be  valid  if all characters of the string appear the same number of times. It is also  valid  if he can remove just   character at   index in the string, and the remaining characters will occur the same number of times. Given a string  , determine if it is  valid . If so, return  YES , otherwise return  NO .   Example This is a valid string because frequencies are  . This is a valid string because we can remove one   and have   of each character in the remaining string. This string is not  valid  as we can only remove   occurrence of  . That leaves character frequencies of  . Sample Input 0 aabbcd Sample Output 0 NO Explanation 0 Given  , we would need to remove two characters, both  c  and  d     aabb  or  a  and  b     abcd , to make it valid. We are limited to removing o...

String Manipulation - Alternating Characters [Easy]

交替字符   hashank非常喜欢字符串,特别是那些连续字符都是不一样的字符串。比如:他喜欢 ,但他不喜欢 。给定一个字符串,该字符串只可能由字母 和 组成。Shashank想把这个字符串转变成他喜欢的字符串,在转变的过程中,他允许删除字符串中的某些字符。 你的任务就是找出最少需要删除几个字符,才能把给定的字符串转变成Shashank喜欢的字符串。 样例输入: 5 AAAA BBBBB ABABABAB BABABA AAABBB 样例输出: 3 4 0 0 4 样例解释: , 需要删除3个字符 , 需要删除4个字符 , 需要删除0个字符 , 需要删除0个字符 , 需要删除4个字符 static   int  alternatingCharacters(String s) {       int  deletions =  0 ;              int  currentCount =  1 ;              for ( int  i =  1 ; i < s.length(); i++)             {                  if (s.charAt(i) != s.charAt(i- 1 ))              ...