📌  相关文章
📜  字符串中最大连续重复字符

📅  最后修改于: 2022-05-13 01:57:06.460000             🧑  作者: Mango

字符串中最大连续重复字符

给定一个字符串,任务是找出一个字符串中最大的连续重复字符。
注意:我们不需要考虑整体计数,而是在一个地方出现的重复计数。
例子:

Input : str = "geeekk"
Output : e

Input : str = "aaaabbcbbb"
Output : a

解决这个问题的简单方法是使用两个 for 循环。外循环考虑当前字符,内循环计算当前字符的出现次数。如果计数超出当前最大计数,我们更新结果。

C++
// C++ program to find the maximum consecutive
// repeating character in given string
#include
using namespace std;
 
// function to find out the maximum repeating
// character in given string
char maxRepeating(string str)
{
    int len = str.length();
    int count = 0;
 
    // Find the maximum repeating character
    // starting from str[i]
    char res = str[0];
    for (int i=0; i count)
        {
            count = cur_count;
            res = str[i];
        }
    }
    return res;
}
 
// Driver code
int main()
{
 
    string str = "aaaabbaaccde";
    cout << maxRepeating(str);
    return 0;
}


Java
// Java program to find the maximum consecutive
// repeating character in given string
public class GFG {
     
    // function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String str)
    {
        int len = str.length();
        int count = 0;
 
        // Find the maximum repeating character
        // starting from str[i]
        char res = str.charAt(0);
        for (int i=0; i count)
            {
                count = cur_count;
                res = str.charAt(i);
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
 
        String str = "aaaabbaaccde";
        System.out.println(maxRepeating(str));
    }
}
// This code is contributed by Sumit Ghosh


Python 3
# Python 3 program to find the
# maximum consecutive repeating
# character in given string
 
# function to find out the maximum
# repeating character in given string
def maxRepeating(str):
 
    l = len(str)
    count = 0
 
    # Find the maximum repeating
    # character starting from str[i]
    res = str[0]
    for i in range(l):
         
        cur_count = 1
        for j in range(i + 1, l):
     
            if (str[i] != str[j]):
                break
            cur_count += 1
 
        # Update result if required
        if cur_count > count :
            count = cur_count
            res = str[i]
    return res
 
# Driver code
if __name__ == "__main__":
 
    str = "aaaabbaaccde"
    print(maxRepeating(str))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find the maximum
// consecutive repeating character
// in given string
using System;
 
class GFG
{
 
// function to find out the maximum
// repeating character in given string
static char maxRepeating(string str)
{
    int len = str.Length;
    int count = 0;
    char res = str[0];
     
    // Find the maximum repeating
    // character starting from str[i]
    for (int i = 0; i < len; i++)
    {
        int cur_count = 1;
        for (int j = i + 1; j < len; j++)
        {
            if (str[i] != str[j])
                break;
            cur_count++;
        }
 
        // Update result if required
        if (cur_count > count)
        {
            count = cur_count;
            res = str[i];
        }
    }
    return res;
}
 
// Driver code
public static void Main()
{
    string str = "aaaabbaaccde";
    Console.Write(maxRepeating(str));
}
}
 
// This code is contributed
// by ChitraNayal


PHP
 $count)
        {
            $count = $cur_count;
            $res = $str[$i];
        }
    }
    return $res;
}
 
// Driver code
    $str = "aaaabbaaccde";
    echo  maxRepeating($str);
 
// This code is contributed by ajit
?>


Javascript


C++
// C++ program to find the maximum consecutive
// repeating character in given string
#include
using namespace std;
 
// Returns the maximum repeating character in a
// given string
char maxRepeating(string str)
{
    int n = str.length();
    int count = 0;
    char res = str[0];
    int cur_count = 1;
 
    // Traverse string except last character
    for (int i=0; i count)
            {
                count = cur_count;
                res = str[i];
            }
            cur_count = 1;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    string str = "aaaabbaaccde";
    cout << maxRepeating(str);
    return 0;
}


Java
// Java program to find the maximum consecutive
// repeating character in given string
class GFG {
     
    // function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String str)
    {
        int n = str.length();
        int count = 0;
        char res = str.charAt(0);
        int cur_count = 1;
 
        // Traverse string except last character
        for (int i = 0; i < n; i++)
        {
            // If current character matches with next
            if (i < n - 1 && str.charAt(i) == str.charAt(i + 1))
                cur_count++;
 
            // If doesn't match, update result
            // (if required) and reset count
            else
            {
                if (cur_count > count)
                {
                    count = cur_count;
                    res = str.charAt(i);
                }
                cur_count = 1;
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String str = "aaaabbaaccde";
        System.out.println(maxRepeating(str));
    }
}
 
// This code is contributed by Sudeep Mukherjee


Python 3
# Python 3 program to find the
# maximum consecutive repeating
# character in given string
 
# Returns the maximum repeating
# character in a given string
def maxRepeating(str):
 
    n = len(str)
    count = 0
    res = str[0]
    cur_count = 1
 
    # Traverse string except
    # last character
    for i in range(n):
         
        # If current character
        # matches with next
        if (i < n - 1 and
            str[i] == str[i + 1]):
            cur_count += 1
 
        # If doesn't match, update result
        # (if required) and reset count
        else:
            if cur_count > count:
                count = cur_count
                res = str[i]
            cur_count = 1
    return res
 
# Driver code
if __name__ == "__main__":
    str = "aaaabbaaccde"
    print(maxRepeating(str))
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find the maximum
// consecutive repeating character
// in given string
using System;
 
class GFG
{
 
// function to find out the
// maximum repeating character
// in given string
static char maxRepeating(string str)
{
    int n = str.Length;
    int count = 0;
    char res = str[0];
    int cur_count = 1;
 
    // Traverse string except
    // last character
    for (int i = 0; i < n; i++)
    {
        // If current character
        // matches with next
        if (i < n - 1 &&
            str[i] == str[i + 1])
            cur_count++;
 
        // If doesn't match, update result
        // (if required) and reset count
        else
        {
            if (cur_count > count)
            {
                count = cur_count;
                res = str[i];
            }
            cur_count = 1;
        }
    }
    return res;
}
 
// Driver code
public static void Main()
{
    string str = "aaaabbaaccde";
    Console.Write(maxRepeating(str));
}
}
 
// This code is contributed
// by ChitraNayal


PHP
 $count)
            {
                $count = $cur_count;
                $res = $str[$i];
            }
            $cur_count = 1;
        }
    }
 
    return $res;
}
 
// Driver code
$str = "aaaabbaaccde";
echo maxRepeating($str);
 
// This code is contributed
// by ChitraNayal
?>


Javascript


输出:

a

时间复杂度: O(n^2)
空间复杂度: O(1)
一种有效的解决方案是只运行一个循环。我们的想法是,一旦我们发现一个与前一个不匹配的字符,就将计数重置为 1。

C++

// C++ program to find the maximum consecutive
// repeating character in given string
#include
using namespace std;
 
// Returns the maximum repeating character in a
// given string
char maxRepeating(string str)
{
    int n = str.length();
    int count = 0;
    char res = str[0];
    int cur_count = 1;
 
    // Traverse string except last character
    for (int i=0; i count)
            {
                count = cur_count;
                res = str[i];
            }
            cur_count = 1;
        }
    }
 
    return res;
}
 
// Driver code
int main()
{
    string str = "aaaabbaaccde";
    cout << maxRepeating(str);
    return 0;
}

Java

// Java program to find the maximum consecutive
// repeating character in given string
class GFG {
     
    // function to find out the maximum repeating
    // character in given string
    static char maxRepeating(String str)
    {
        int n = str.length();
        int count = 0;
        char res = str.charAt(0);
        int cur_count = 1;
 
        // Traverse string except last character
        for (int i = 0; i < n; i++)
        {
            // If current character matches with next
            if (i < n - 1 && str.charAt(i) == str.charAt(i + 1))
                cur_count++;
 
            // If doesn't match, update result
            // (if required) and reset count
            else
            {
                if (cur_count > count)
                {
                    count = cur_count;
                    res = str.charAt(i);
                }
                cur_count = 1;
            }
        }
        return res;
    }
 
    // Driver code
    public static void main(String args[])
    {
        String str = "aaaabbaaccde";
        System.out.println(maxRepeating(str));
    }
}
 
// This code is contributed by Sudeep Mukherjee

Python3

# Python 3 program to find the
# maximum consecutive repeating
# character in given string
 
# Returns the maximum repeating
# character in a given string
def maxRepeating(str):
 
    n = len(str)
    count = 0
    res = str[0]
    cur_count = 1
 
    # Traverse string except
    # last character
    for i in range(n):
         
        # If current character
        # matches with next
        if (i < n - 1 and
            str[i] == str[i + 1]):
            cur_count += 1
 
        # If doesn't match, update result
        # (if required) and reset count
        else:
            if cur_count > count:
                count = cur_count
                res = str[i]
            cur_count = 1
    return res
 
# Driver code
if __name__ == "__main__":
    str = "aaaabbaaccde"
    print(maxRepeating(str))
 
# This code is contributed
# by ChitraNayal

C#

// C# program to find the maximum
// consecutive repeating character
// in given string
using System;
 
class GFG
{
 
// function to find out the
// maximum repeating character
// in given string
static char maxRepeating(string str)
{
    int n = str.Length;
    int count = 0;
    char res = str[0];
    int cur_count = 1;
 
    // Traverse string except
    // last character
    for (int i = 0; i < n; i++)
    {
        // If current character
        // matches with next
        if (i < n - 1 &&
            str[i] == str[i + 1])
            cur_count++;
 
        // If doesn't match, update result
        // (if required) and reset count
        else
        {
            if (cur_count > count)
            {
                count = cur_count;
                res = str[i];
            }
            cur_count = 1;
        }
    }
    return res;
}
 
// Driver code
public static void Main()
{
    string str = "aaaabbaaccde";
    Console.Write(maxRepeating(str));
}
}
 
// This code is contributed
// by ChitraNayal

PHP

 $count)
            {
                $count = $cur_count;
                $res = $str[$i];
            }
            $cur_count = 1;
        }
    }
 
    return $res;
}
 
// Driver code
$str = "aaaabbaaccde";
echo maxRepeating($str);
 
// This code is contributed
// by ChitraNayal
?>

Javascript


输出:

a