Check if all occurrences of a character appear together
Last Updated :
12 Aug, 2022
Given a string s and a character c, find if all occurrences of c appear together in s or not. If the character c does not appear in the string at all, the answer is true.
Examples
Input: s = "1110000323", c = '1'
Output: Yes
All occurrences of '1' appear together in
"1110000323"
Input: s = "3231131", c = '1'
Output: No
All occurrences of 1 are not together
Input: s = "abcabc", c = 'c'
Output: No
All occurrences of 'c' are not together
Input: s = "ababcc", c = 'c'
Output: Yes
All occurrences of 'c' are together
The idea is to traverse given string, as soon as we find an occurrence of c, we keep traversing until we find a character which is not c. We also set a flag to indicate that one more occurrences of c are seen. If we see c again and flag is set, then we return false.
Implementation:
C++
#include <iostream>
#include <string>
using namespace std;
bool checkIfAllTogether(string s, char c)
{
bool oneSeen = false ;
int i = 0, n = s.length();
while (i < n) {
if (s[i] == c) {
if (oneSeen == true )
return false ;
while (i < n && s[i] == c)
i++;
oneSeen = true ;
}
else
i++;
}
return true ;
}
int main()
{
string s = "110029" ;
if (checkIfAllTogether(s, '1' ))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean checkIfAllTogether(String s,
char c)
{
boolean oneSeen = false ;
int i = 0 , n = s.length();
while (i < n)
{
if (s.charAt(i) == c)
{
if (oneSeen == true )
return false ;
while (i < n && s.charAt(i) == c)
i++;
oneSeen = true ;
}
else
i++;
}
return true ;
}
public static void main(String[] args)
{
String s = "110029" ;
if (checkIfAllTogether(s, '1' ))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def checkIfAllTogether(s, c) :
oneSeen = False
i = 0
n = len (s)
while (i < n) :
if (s[i] = = c) :
if (oneSeen = = True ) :
return False
while (i < n and s[i] = = c) :
i = i + 1
oneSeen = True
else :
i = i + 1
return True
s = "110029" ;
if (checkIfAllTogether(s, '1' )) :
print ( "Yes\n" )
else :
print ( "No\n" )
|
C#
using System;
public class GFG {
static bool checkIfAllTogether( string s,
char c)
{
bool oneSeen = false ;
int i = 0, n = s.Length;
while (i < n) {
if (s[i] == c) {
if (oneSeen == true )
return false ;
while (i < n && s[i] == c)
i++;
oneSeen = true ;
}
else
i++;
}
return true ;
}
public static void Main()
{
string s = "110029" ;
if (checkIfAllTogether(s, '1' ))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function checkIfAllTogether( $s , $c )
{
$oneSeen = false;
$i = 0; $n = strlen ( $s );
while ( $i < $n )
{
if ( $s [ $i ] == $c )
{
if ( $oneSeen == true)
return false;
while ( $i < $n && $s [ $i ] == $c )
$i ++;
$oneSeen = true;
}
else
$i ++;
}
return true;
}
$s = "110029" ;
if (checkIfAllTogether( $s , '1' ))
echo ( "Yes\n" );
else
echo ( "No\n" );
?>
|
Javascript
<script>
function checkIfAllTogether(s, c)
{
let oneSeen = false ;
let i = 0, n = s.length;
while (i < n)
{
if (s[i] == c)
{
if (oneSeen == true )
return false ;
while (i < n && s[i] == c)
i++;
oneSeen = true ;
}
else
i++;
}
return true ;
}
let s = "110029" ;
if (checkIfAllTogether(s, '1' ))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Complexity Analysis:
- Time Complexity: O(n), where n is the number of characters in the string.
- Auxiliary Space: O(1),
Please suggest if someone has a better solution which is more efficient in terms of space and time.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...