Eliminar dígitos recurrentes en un número dado

Dado un número como string, elimine los dígitos recurrentes de la string dada. Los cambios deben hacerse en el lugar. Complejidad temporal esperada O(n) y espacio auxiliar O(1).
Ejemplos: 
 

Input:  num[] = "1299888833"
Output: num[] = "12983"

Input:  num[] = "1299888833222"
Output: num[] = "129832"

Le recomendamos encarecidamente que minimice su navegador y que pruebe esto usted mismo primero
. Este problema es similar a la codificación de longitud de ejecución
 

C++

// C++ program to remove recurring digits from
// a given number
#include <bits/stdc++.h>
using namespace std;
 
/* Removes recurring digits in num[]  */
void removeRecurringDigits(char num[])
{
    int len = strlen(num);
 
    int j = 0; // Index in modified string
 
    /* Traverse digits of given number one by one */
    for (int i=0; i<len; i++)
    {
        /* Copy the first occurrence of new digit */
        num[j++] = num[i];
 
        /* Remove repeating occurrences of digit */
        while (i + 1 < len && num[i] == num[i+1])
            i++;
    }
 
    /* terminate the modified string */
   num[j] = '\0';
}
 
/* Driver program to test above function */
int main()
{
    char num[] = "1299888833";
    removeRecurringDigits(num);
    cout << "Modified number is " << num;
    return 0;
}

Java

// Java program to remove recurring
// digits from a given number
class GFG
{
 
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char num[])
    {
        int len = num.length;
 
        int j = 0; // Index in modified string
        String s = "";
         
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++)
        {
             
            /* Copy the first occurrence of new digit */
            s += String.valueOf(num[i]);
 
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1])
            {
                i++;
            }
        }
        return s;
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        char num[] = "1299888833".toCharArray();
        System.out.print("Modified number is " +
                        removeRecurringDigits(num));
    }
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python3 program to remove recurring
# digits from a given number
 
# Removes recurring digits in num[]
def removeRecurringDigits(num):
 
    l = len(num)
     
    # Index in modified string
    (i, j) = (0, 0)
    str = ''
     
    # Traverse digits of given
    # number one by one
    while i < l:
         
        # Copy the first occurrence
        # of new digit
        str += num[i]
        j += 1
         
        # Remove repeating occurrences of digit
        while (i + 1 < l and num[i] == num[i + 1]):
            i += 1
        i += 1
 
    return str
 
# Driver code
if __name__ == '__main__':
     
    num = '1299888833'
    print('Modified number is {}'.format(
           removeRecurringDigits(num)))
 
# This code is contributed by rutvik_56

C#

// C# program to remove recurring
// digits from a given number
using System;
 
class GFG
{
 
    /* Removes recurring digits in num[] */
    static String removeRecurringDigits(char []num)
    {
        int len = num.Length;
 
        int j = 0; // Index in modified string
        String s = "";
         
        /* Traverse digits of given number one by one */
        for (int i = 0; i < len; i++)
        {
             
            /* Copy the first occurrence of new digit */
            s += String.Join("",num[i]);
 
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1])
            {
                i++;
            }
        }
        return s;
    }
 
    /* Driver code */
    public static void Main()
    {
        char []num = "1299888833".ToCharArray();
        Console.Write("Modified number is " +
                        removeRecurringDigits(num));
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript

<script>
// Javascript program to remove recurring
// digits from a given number
 
 /* Removes recurring digits in num[] */
function removeRecurringDigits(num)
{
    let len = num.length;
   
        let j = 0; // Index in modified string
        let s = "";
           
        /* Traverse digits of given number one by one */
        for (let i = 0; i < len; i++)
        {
               
            /* Copy the first occurrence of new digit */
            s += (num[i]);
   
            /* Remove repeating occurrences of digit */
            while (i + 1 < len && num[i] == num[i + 1])
            {
                i++;
            }
        }
        return s;
}
 
/* Driver code */
let num="1299888833".split("");
document.write("Modified number is " +
                        removeRecurringDigits(num));
 
 
// This code is contributed by rag2127
</script>

C++

//C++ program to remove reccuring
//degits in a given number
#include<bits/stdc++.h>
using namespace std;
void updatestring(string s){
    int x=s.length()-2;
    //traversing the string
    for(int i=0;i<x;i++){
         
        if(i>s.length()-2){
            break;
        }
        if(s[i]==s[i+1]){
            //removing character which is
            //occurring more then one time
            s.erase(s.begin()+i);
            i--;
        }
    }
    cout<<"number without reccuring degits: "<<s<<endl;
}
//driver code
int main() {
string s="1299888833222";
//function call
updatestring(s);
return 0;
}
 
//this code is contribut by Machhaliya Muhammad

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *