📌  相关文章
📜  在字符串找到一个额外的字符

📅  最后修改于: 2021-05-25 10:31:39             🧑  作者: Mango

给定两个长度为n和n + 1的字符串。第二个字符串包含第一个字符串的所有字符,但是还有一个额外的字符。您的任务是在第二个字符串查找额外的字符。

例子 :

Input : string strA = "abcd";
        string strB = "cbdae";
Output : e
string B contain all the element 
there is a one extra character which is e

Input : string strA = "kxml";
        string strB = "klxml";
Output : l
string B contain all the element 
there is a one extra character which is l

方法1(蛮力):-
检查两个是否为循环。
时间复杂度:-O(n ^ 2)
空间复杂度:-O(1)。

方法2(哈希图):-
创建一个空的哈希表,并插入第二个字符串的所有字符。现在删除第一个字符串的所有字符。剩下的字符就是多余的字符。

时间复杂度:-O(n)
辅助空间:-O(n)。

C++
// CPP program to find extra character in one 
// string
#include 
using namespace std;
  
char findExtraCharcter(string strA, string strB)
{
    // store string values in map
    unordered_map m1;
  
    // store second string in map with frequency
    for (int i = 0; i < strB.length(); i++)
        m1[strB[i]]++;
  
    // store first string in map with frequency
    for (int i = 0; i < strA.length(); i++)
        m1[strA[i]]--;
  
    for (auto h1 = m1.begin(); h1 != m1.end(); h1++) {
  
        // if the frequency is 1 then this
        // character is which is added extra
        if (h1->second == 1)
            return h1->first;
    }
}
  
int main()
{
    // given string
    string strA = "abcd";
    string strB = "cbdad";
  
    // find Extra Character
    cout << findExtraCharcter(strA, strB);
}


Java
// Java program to find extra character in one 
// string
class GFG 
{
  
static char findExtraCharcter(char []strA, char[] strB)
{
    // store string values in map
    int[] m1 = new int[256];
  
    // store second string in map with frequency
    for (int i = 0; i < strB.length; i++)
        m1[strB[i]]++;
  
    // store first string in map with frequency
    for (int i = 0; i < strA.length; i++)
        m1[strA[i]]--;
  
    for (int i=0;i


Python3
# Python3 program to find extra character 
# in one string
def findExtraCharacter(strA, strB):
  
    # store string values in map
    m1 = {}
  
    # store second string in map 
    # with frequency
    for i in strB:
        if i in m1:
            m1[i] += 1
        else:
            m1[i] = 1
  
    # store first string in map 
    # with frequency
    for i in strA:
        m1[i] -= 1
  
    for h1 in m1:
  
        # if the frequency is 1 then this
        # character is which is added extra
        if m1[h1] == 1:
            return h1
  
# Driver Code
if __name__ == "__main__":
  
    # given string
    strA = 'abcd'
    strB = 'cbdad'
  
    # find Extra Character
    print(findExtraCharacter(strA, strB))
  
# This code is contributed by
# sanjeev2552


C#
// C# program to find extra character in one 
// string
using System;
      
class GFG 
{
  
static char findExtraCharcter(char []strA, char[] strB)
{
    // store string values in map
    int[] m1 = new int[256];
  
    // store second string in map with frequency
    for (int i = 0; i < strB.Length; i++)
        m1[strB[i]]++;
  
    // store first string in map with frequency
    for (int i = 0; i < strA.Length; i++)
        m1[strA[i]]--;
  
    for (int i = 0; i < m1.Length; i++)
    {
  
        // if the frequency is 1 then this
        // character is which is added extra
        if (m1[i]== 1)
            return (char) i;
    }
    return char.MinValue;
}
  
// Driver code
public static void Main(String[] args) 
{
    // given string
    String strA = "abcd";
    String strB = "cbdad";
  
    // find Extra Character
    Console.WriteLine(findExtraCharcter(strA.ToCharArray(), 
                                        strB.ToCharArray()));
    }
}
  
// This code is contributed by Rajput-Ji


C++
// CPP program to find extra character in one 
// string
#include 
using namespace std;
  
char findExtraCharcter(string strA, string strB)
{
    // result store the result
    int res = 0, i;
  
    // traverse string A till end and 
    // xor with res
    for (i = 0; i < strA.length(); i++) {
  
        // xor with res
        res ^= strA[i];
    }
  
    // traverse string B till end and 
    // xor with res
    for (i = 0; i < strB.length(); i++) {
  
        // xor with res
        res ^= strB[i];
    }
  
    // print result at the end
    return ((char)(res));
}
  
int main()
{
    // given string
    string strA = "abcd";
    string strB = "cbdad";
    cout << findExtraCharcter(strA, strB);
    return 0;
}


Java
// Java program to find extra
// character in one string
import java.io.*;
  
class GFG {
      
    static char findExtraCharcter(String strA,
                                  String strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till 
        // end and xor with res
        for (i = 0; i < strA.length(); i++)
        {
            // xor with res
            res ^= strA.charAt(i);
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.length(); i++)
        {
            // xor with res
            res ^= strB.charAt(i);
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver code
    public static void main(String args[])
    {
        // given string
        String strA = "abcd";
        String strB = "cbdad";
        System.out.println(findExtraCharcter(strA, strB));
    }
}
  
/*This code is contributed by Nikita Tiwari.*/


Python 3
# Python 3 program to find 
# extra character in one string
  
def findExtraCharcter(strA, strB) :
      
    # result store the result
    res = 0
  
    # traverse string A till 
    # end and xor with res
    for i in range(0,len(strA)) :
          
        # xor with res
        res =res ^ (ord)(strA[i])
          
    # traverse string B till 
    # end and xor with res
    for i in range(0,len(strB)) :
          
        # xor with res
        res = res ^ (ord)(strB[i])
          
    # print result at the end
    return ((chr)(res));
  
# given string
strA = "abcd"
strB = "cbdad"
print(findExtraCharcter(strA, strB))
  
# This code is contributed by Nikita Tiwari.


C#
// C# program to find extra character
// in one string
using System;
  
class GFG {
  
    static char findExtraCharcter(string strA, 
                                  string strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till end and 
        // xor with res
        for (i = 0; i < strA.Length; i++) {
      
            // xor with res
            res ^= strA[i];
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.Length; i++) {
      
            // xor with res
            res ^= strB[i];
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver Code
    public static void Main()
    {
        // given string
        string strA = "abcd";
        string strB = "cbdad";
        Console.WriteLine(
            findExtraCharcter(strA, strB));
    }
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)


PHP


C++
// C++ program to find extra
// character in one string
#include
using namespace std;
  
char findExtraCharacter(string s1, string s2)
{     
    string smallStr;
    string largeStr;
  
    // Determine string with extra character.
    if(s1.size() > s2.size()) 
    {
        smallStr = s2;
        largeStr = s1;
    } 
    else
    {
        smallStr = s1;
        largeStr = s2;
    }
  
    int smallStrCodeTotal = 0;
    int largeStrCodeTotal = 0;
    int i = 0;
  
    // Add character codes of both the strings
    for(; i < smallStr.size(); i++) 
    {
        smallStrCodeTotal += smallStr[i];
        largeStrCodeTotal += largeStr[i];
    }
  
    // Add last character code of large string.
    largeStrCodeTotal += largeStr[i];
  
    // Minus the character code of smaller string from 
    // the character code of large string. 
    // The result will be the extra character code.
    int intChar = largeStrCodeTotal - smallStrCodeTotal;     
    return (char)intChar;
}
  
// Driver code 
int main()
{
    string s1 = "abcd";
    string s2 = "cbdae";
      
    char extraChar = findExtraCharacter(s1, s2);
    cout<<"Extra character: " <<(extraChar)<


Java
// Java program to find extra
// character in one string
  
public class Test {
  
    private static char findExtraCharacter(String s1, String s2) {        
        String smallStr;
        String largeStr;
  
        // Determine String with extra character.
        if(s1.length() > s2.length()) {
            smallStr = s2;
            largeStr = s1;
        } else {
            smallStr = s1;
            largeStr = s2;
        }
  
        int smallStrCodeTotal = 0;
        int largeStrCodeTotal = 0;
        int i = 0;
  
        // Add character codes of both the strings
        for(; i < smallStr.length(); i++) {
            smallStrCodeTotal += smallStr.charAt(i);
            largeStrCodeTotal += largeStr.charAt(i);
        }
  
        // Add last character code of large String.
        largeStrCodeTotal += largeStr.charAt(i);
  
        // Minus the character code of smaller string from 
        // the character code of large string. 
        // The result will be the extra character code.
        int intChar = largeStrCodeTotal - smallStrCodeTotal;        
        return (char)intChar;
    }
      
    public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "cbdae";
          
        char extraChar = findExtraCharacter(s1, s2);
        System.out.println("Extra character: " + extraChar);
          
    }
}
  
  
/*This code is contributed by Amol Bhosale.*/


Python3
# Python Program to find extra character in one string 
def findExtraCharacter(s1,s2):
    smallStr = ""
    largeStr = ""
      
    # Determine string with extra character
    if(len(s1) > len(s2)):
        smallStr = s2
        largeStr = s1
    else:
        smallStr = s1
        largeStr = s2
    smallStrCodeTotal = 0
    largeStrCodeTotal = 0
    i = 0
      
    # Add Character codes of both the strings
    while(i < len(smallStr)):
        smallStrCodeTotal += ord(smallStr[i])
        largeStrCodeTotal += ord(largeStr[i])
        i += 1
      
    # Add last character code of large string
    largeStrCodeTotal += ord(largeStr[i])
      
    # Minus the character code of smaller string 
    # from the character code of large string
    # The result will be the extra character code
    intChar = largeStrCodeTotal - smallStrCodeTotal
    return chr(intChar)
  
# Driver code
s1 = "abcd"
s2 = "cbdae"
extraChar = findExtraCharacter(s1, s2)
print("Extra Character:", extraChar)
  
# This code is contributed by simranjenny84


C#
// C# program to find extra 
// character in one string 
using System;
  
class GFG 
{ 
    private static char findExtraCharacter(String s1, 
                                           String s2) 
    {     
        String smallStr; 
        String largeStr; 
  
        // Determine String with extra character. 
        if(s1.Length > s2.Length) 
        { 
            smallStr = s2; 
            largeStr = s1; 
        } 
        else 
        { 
            smallStr = s1; 
            largeStr = s2; 
        } 
  
        int smallStrCodeTotal = 0; 
        int largeStrCodeTotal = 0; 
        int i = 0; 
  
        // Add character codes of both the strings 
        for(; i < smallStr.Length; i++) 
        { 
            smallStrCodeTotal += smallStr[i]; 
            largeStrCodeTotal += largeStr[i]; 
        } 
  
        // Add last character code of large String. 
        largeStrCodeTotal += largeStr[i]; 
  
        // Minus the character code of smaller string 
        // from the character code of large string. 
        // The result will be the extra character code. 
        int intChar = largeStrCodeTotal - 
                      smallStrCodeTotal;         
        return (char)intChar; 
    } 
      
    public static void Main(String[] args)
    { 
        String s1 = "abcd"; 
        String s2 = "cbdae"; 
          
        char extraChar = findExtraCharacter(s1, s2); 
        Console.WriteLine("Extra character: " + 
                                    extraChar); 
    } 
} 
  
// This code is contributed by PrinciRaj1992


输出:
d

方法3(位):-
从xor运算开始遍历第一个和第二个字符串,最后得到多余的字符。
时间复杂度:-O(n + n + 1)
空间复杂度:-O(1)。

C++

// CPP program to find extra character in one 
// string
#include 
using namespace std;
  
char findExtraCharcter(string strA, string strB)
{
    // result store the result
    int res = 0, i;
  
    // traverse string A till end and 
    // xor with res
    for (i = 0; i < strA.length(); i++) {
  
        // xor with res
        res ^= strA[i];
    }
  
    // traverse string B till end and 
    // xor with res
    for (i = 0; i < strB.length(); i++) {
  
        // xor with res
        res ^= strB[i];
    }
  
    // print result at the end
    return ((char)(res));
}
  
int main()
{
    // given string
    string strA = "abcd";
    string strB = "cbdad";
    cout << findExtraCharcter(strA, strB);
    return 0;
}

Java

// Java program to find extra
// character in one string
import java.io.*;
  
class GFG {
      
    static char findExtraCharcter(String strA,
                                  String strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till 
        // end and xor with res
        for (i = 0; i < strA.length(); i++)
        {
            // xor with res
            res ^= strA.charAt(i);
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.length(); i++)
        {
            // xor with res
            res ^= strB.charAt(i);
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver code
    public static void main(String args[])
    {
        // given string
        String strA = "abcd";
        String strB = "cbdad";
        System.out.println(findExtraCharcter(strA, strB));
    }
}
  
/*This code is contributed by Nikita Tiwari.*/

的Python 3

# Python 3 program to find 
# extra character in one string
  
def findExtraCharcter(strA, strB) :
      
    # result store the result
    res = 0
  
    # traverse string A till 
    # end and xor with res
    for i in range(0,len(strA)) :
          
        # xor with res
        res =res ^ (ord)(strA[i])
          
    # traverse string B till 
    # end and xor with res
    for i in range(0,len(strB)) :
          
        # xor with res
        res = res ^ (ord)(strB[i])
          
    # print result at the end
    return ((chr)(res));
  
# given string
strA = "abcd"
strB = "cbdad"
print(findExtraCharcter(strA, strB))
  
# This code is contributed by Nikita Tiwari.

C#

// C# program to find extra character
// in one string
using System;
  
class GFG {
  
    static char findExtraCharcter(string strA, 
                                  string strB)
    {
        // result store the result
        int res = 0, i;
      
        // traverse string A till end and 
        // xor with res
        for (i = 0; i < strA.Length; i++) {
      
            // xor with res
            res ^= strA[i];
        }
      
        // traverse string B till end and 
        // xor with res
        for (i = 0; i < strB.Length; i++) {
      
            // xor with res
            res ^= strB[i];
        }
      
        // print result at the end
        return ((char)(res));
    }
      
    // Driver Code
    public static void Main()
    {
        // given string
        string strA = "abcd";
        string strB = "cbdad";
        Console.WriteLine(
            findExtraCharcter(strA, strB));
    }
}
  
// This code is contributed by Manish Shaw 
// (manishshaw1)

的PHP


输出:
d

方法4(字符代码):-
添加两个字符串的字符代码。减去较大字符串中较小字符串的字符代码,然后将结果整数转换为字符。

时间复杂度: -O(n)
辅助空间: -O(1)。

C++

// C++ program to find extra
// character in one string
#include
using namespace std;
  
char findExtraCharacter(string s1, string s2)
{     
    string smallStr;
    string largeStr;
  
    // Determine string with extra character.
    if(s1.size() > s2.size()) 
    {
        smallStr = s2;
        largeStr = s1;
    } 
    else
    {
        smallStr = s1;
        largeStr = s2;
    }
  
    int smallStrCodeTotal = 0;
    int largeStrCodeTotal = 0;
    int i = 0;
  
    // Add character codes of both the strings
    for(; i < smallStr.size(); i++) 
    {
        smallStrCodeTotal += smallStr[i];
        largeStrCodeTotal += largeStr[i];
    }
  
    // Add last character code of large string.
    largeStrCodeTotal += largeStr[i];
  
    // Minus the character code of smaller string from 
    // the character code of large string. 
    // The result will be the extra character code.
    int intChar = largeStrCodeTotal - smallStrCodeTotal;     
    return (char)intChar;
}
  
// Driver code 
int main()
{
    string s1 = "abcd";
    string s2 = "cbdae";
      
    char extraChar = findExtraCharacter(s1, s2);
    cout<<"Extra character: " <<(extraChar)<

Java

// Java program to find extra
// character in one string
  
public class Test {
  
    private static char findExtraCharacter(String s1, String s2) {        
        String smallStr;
        String largeStr;
  
        // Determine String with extra character.
        if(s1.length() > s2.length()) {
            smallStr = s2;
            largeStr = s1;
        } else {
            smallStr = s1;
            largeStr = s2;
        }
  
        int smallStrCodeTotal = 0;
        int largeStrCodeTotal = 0;
        int i = 0;
  
        // Add character codes of both the strings
        for(; i < smallStr.length(); i++) {
            smallStrCodeTotal += smallStr.charAt(i);
            largeStrCodeTotal += largeStr.charAt(i);
        }
  
        // Add last character code of large String.
        largeStrCodeTotal += largeStr.charAt(i);
  
        // Minus the character code of smaller string from 
        // the character code of large string. 
        // The result will be the extra character code.
        int intChar = largeStrCodeTotal - smallStrCodeTotal;        
        return (char)intChar;
    }
      
    public static void main(String[] args) {
        String s1 = "abcd";
        String s2 = "cbdae";
          
        char extraChar = findExtraCharacter(s1, s2);
        System.out.println("Extra character: " + extraChar);
          
    }
}
  
  
/*This code is contributed by Amol Bhosale.*/

Python3

# Python Program to find extra character in one string 
def findExtraCharacter(s1,s2):
    smallStr = ""
    largeStr = ""
      
    # Determine string with extra character
    if(len(s1) > len(s2)):
        smallStr = s2
        largeStr = s1
    else:
        smallStr = s1
        largeStr = s2
    smallStrCodeTotal = 0
    largeStrCodeTotal = 0
    i = 0
      
    # Add Character codes of both the strings
    while(i < len(smallStr)):
        smallStrCodeTotal += ord(smallStr[i])
        largeStrCodeTotal += ord(largeStr[i])
        i += 1
      
    # Add last character code of large string
    largeStrCodeTotal += ord(largeStr[i])
      
    # Minus the character code of smaller string 
    # from the character code of large string
    # The result will be the extra character code
    intChar = largeStrCodeTotal - smallStrCodeTotal
    return chr(intChar)
  
# Driver code
s1 = "abcd"
s2 = "cbdae"
extraChar = findExtraCharacter(s1, s2)
print("Extra Character:", extraChar)
  
# This code is contributed by simranjenny84

C#

// C# program to find extra 
// character in one string 
using System;
  
class GFG 
{ 
    private static char findExtraCharacter(String s1, 
                                           String s2) 
    {     
        String smallStr; 
        String largeStr; 
  
        // Determine String with extra character. 
        if(s1.Length > s2.Length) 
        { 
            smallStr = s2; 
            largeStr = s1; 
        } 
        else 
        { 
            smallStr = s1; 
            largeStr = s2; 
        } 
  
        int smallStrCodeTotal = 0; 
        int largeStrCodeTotal = 0; 
        int i = 0; 
  
        // Add character codes of both the strings 
        for(; i < smallStr.Length; i++) 
        { 
            smallStrCodeTotal += smallStr[i]; 
            largeStrCodeTotal += largeStr[i]; 
        } 
  
        // Add last character code of large String. 
        largeStrCodeTotal += largeStr[i]; 
  
        // Minus the character code of smaller string 
        // from the character code of large string. 
        // The result will be the extra character code. 
        int intChar = largeStrCodeTotal - 
                      smallStrCodeTotal;         
        return (char)intChar; 
    } 
      
    public static void Main(String[] args)
    { 
        String s1 = "abcd"; 
        String s2 = "cbdae"; 
          
        char extraChar = findExtraCharacter(s1, s2); 
        Console.WriteLine("Extra character: " + 
                                    extraChar); 
    } 
} 
  
// This code is contributed by PrinciRaj1992
输出:
Extra character: e