Check if a string contains all unique characters
Here given code implementation process.
// C Program for
// Check if a string contains all unique characters
#include <stdio.h>
#include <string.h>
void isUniqueCharacters(char *text)
{
int n = strlen(text);
if(n == 0)
{
// Empty String
return;
}
int characters[255];
// Set initial frequency
for (int i = 0; i < 255; ++i)
{
characters[i] = 0;
}
printf("\n Given text : %s ", text);
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
characters[text[i]-'0'] ++;
if(characters[text[i]-'0'] > 1)
{
// When characters are repeated
printf("\n No ");
return;
}
}
// When all characters unique
printf("\n Yes ");
}
int main(int argc, char const *argv[])
{
// Test
isUniqueCharacters("xyzklsadfl");
isUniqueCharacters("3424789dfkj");
isUniqueCharacters("kalicodes");
isUniqueCharacters("908");
return 0;
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
// Java program for
// Check if a string contains all unique characters
public class UniqueCharacters
{
public void isUniqueCharacters(String text)
{
int n = text.length();
if (n == 0)
{
// Empty String
return;
}
boolean[] characters = new boolean[255];
// Set initial frequency
for (int i = 0; i < 255; ++i)
{
characters[i] = false;
}
System.out.print("\n Given text : " + text);
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
if (characters[text.charAt(i) - '0'] == true)
{
// When characters are repeated
System.out.print("\n No ");
return;
}
else
{
characters[text.charAt(i) - '0'] = true;
}
}
// When all characters unique
System.out.print("\n Yes ");
}
public static void main(String[] args)
{
UniqueCharacters task = new UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program for
// Check if a string contains all unique characters
class UniqueCharacters
{
public: void isUniqueCharacters(string text)
{
int n = text.length();
if (n == 0)
{
// Empty String
return;
}
bool characters[255];
// Set initial frequency
for (int i = 0; i < 255; ++i)
{
characters[i] = false;
}
cout << "\n Given text : " << text;
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
if (characters[text[i] - '0'] == true)
{
// When characters are repeated
cout << "\n No ";
return;
}
else
{
characters[text[i] - '0'] = true;
}
}
// When all characters unique
cout << "\n Yes ";
}
};
int main()
{
UniqueCharacters *task = new UniqueCharacters();
// Test
task->isUniqueCharacters("xyzklsadfl");
task->isUniqueCharacters("3424789dfkj");
task->isUniqueCharacters("kalicodes");
task->isUniqueCharacters("908");
return 0;
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
package main
import "fmt"
// Go program for
// Check if a string contains all unique characters
type UniqueCharacters struct {}
func getUniqueCharacters() * UniqueCharacters {
var me *UniqueCharacters = &UniqueCharacters {}
return me
}
func(this UniqueCharacters) isUniqueCharacters(text string) {
var n int = len(text)
if n == 0 {
// Empty String
return
}
var characters = make([] bool, 255)
// Set initial frequency
for i := 0 ; i < 255 ; i++ {
characters[i] = false
}
fmt.Print("\n Given text : ", text)
// Count frequency of character element
for i := 0 ; i < n ; i++ {
if characters[text[i] - '0'] == true {
// When characters are repeated
fmt.Print("\n No ")
return
} else {
characters[text[i] - '0'] = true
}
}
// When all characters unique
fmt.Print("\n Yes ")
}
func main() {
var task * UniqueCharacters = getUniqueCharacters()
// Test
task.isUniqueCharacters("xyzklsadfl")
task.isUniqueCharacters("3424789dfkj")
task.isUniqueCharacters("kalicodes")
task.isUniqueCharacters("908")
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
// Include namespace system
using System;
// Csharp program for
// Check if a string contains all unique characters
public class UniqueCharacters
{
public void isUniqueCharacters(String text)
{
int n = text.Length;
if (n == 0)
{
// Empty String
return;
}
Boolean[] characters = new Boolean[255];
// Set initial frequency
for (int i = 0; i < 255; ++i)
{
characters[i] = false;
}
Console.Write("\n Given text : " + text);
// Count frequency of character element
for (int i = 0; i < n; ++i)
{
if (characters[text[i] - '0'] == true)
{
// When characters are repeated
Console.Write("\n No ");
return;
}
else
{
characters[text[i] - '0'] = true;
}
}
// When all characters unique
Console.Write("\n Yes ");
}
public static void Main(String[] args)
{
UniqueCharacters task = new UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
<?php
// Php program for
// Check if a string contains all unique characters
class UniqueCharacters
{
public function isUniqueCharacters($text)
{
$n = strlen($text);
if ($n == 0)
{
// Empty String
return;
}
$characters = array_fill(0, 255, false);
echo("\n Given text : ".$text);
// Count frequency of character element
for ($i = 0; $i < $n; ++$i)
{
if ($characters[ord($text[$i]) - ord('0')] == true)
{
// When characters are repeated
echo("\n No ");
return;
}
else
{
$characters[ord($text[$i]) - ord('0')] = true;
}
}
// When all characters unique
echo("\n Yes ");
}
}
function main()
{
$task = new UniqueCharacters();
// Test
$task->isUniqueCharacters("xyzklsadfl");
$task->isUniqueCharacters("3424789dfkj");
$task->isUniqueCharacters("kalicodes");
$task->isUniqueCharacters("908");
}
main();
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
// Node JS program for
// Check if a string contains all unique characters
class UniqueCharacters
{
isUniqueCharacters(text)
{
var n = text.length;
if (n == 0)
{
// Empty String
return;
}
var characters = Array(255).fill(false);
process.stdout.write("\n Given text : " + text);
// Count frequency of character element
for (var i = 0; i < n; ++i)
{
if (characters[text.charCodeAt(i) - '0'.charCodeAt(0)] == true)
{
// When characters are repeated
process.stdout.write("\n No ");
return;
}
else
{
characters[text.charCodeAt(i) - '0'.charCodeAt(0)] = true;
}
}
// When all characters unique
process.stdout.write("\n Yes ");
}
}
function main()
{
var task = new UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
main();
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
# Python 3 program for
# Check if a string contains all unique characters
class UniqueCharacters :
def isUniqueCharacters(self, text) :
n = len(text)
if (n == 0) :
# Empty String
return
characters = [False] * (255)
print("\n Given text : ", text, end = "")
i = 0
# Count frequency of character element
while (i < n) :
if (characters[ord(text[i]) - ord('0')] == True) :
# When characters are repeated
print("\n No ", end = "")
return
else :
characters[ord(text[i]) - ord('0')] = True
i += 1
# When all characters unique
print("\n Yes ", end = "")
def main() :
task = UniqueCharacters()
# Test
task.isUniqueCharacters("xyzklsadfl")
task.isUniqueCharacters("3424789dfkj")
task.isUniqueCharacters("kalicodes")
task.isUniqueCharacters("908")
if __name__ == "__main__": main()
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
# Ruby program for
# Check if a string contains all unique characters
class UniqueCharacters
def isUniqueCharacters(text)
n = text.length
if (n == 0)
# Empty String
return
end
characters = Array.new(255) {false}
print("\n Given text : ", text)
i = 0
# Count frequency of character element
while (i < n)
if (characters[text[i].ord - '0'.ord] == true)
# When characters are repeated
print("\n No ")
return
else
characters[text[i].ord - '0'.ord] = true
end
i += 1
end
# When all characters unique
print("\n Yes ")
end
end
def main()
task = UniqueCharacters.new()
# Test
task.isUniqueCharacters("xyzklsadfl")
task.isUniqueCharacters("3424789dfkj")
task.isUniqueCharacters("kalicodes")
task.isUniqueCharacters("908")
end
main()
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
import scala.collection.mutable._;
// Scala program for
// Check if a string contains all unique characters
class UniqueCharacters()
{
def isUniqueCharacters(text: String): Unit = {
var n: Int = text.length();
if (n == 0)
{
// Empty String
return;
}
var characters: Array[Boolean] = Array.fill[Boolean](255)(false);
print("\n Given text : " + text);
var i: Int = 0;
// Count frequency of character element
while (i < n)
{
if (characters(text.charAt(i).toInt - '0'.toInt) == true)
{
// When characters are repeated
print("\n No ");
return;
}
else
{
characters(text.charAt(i).toInt - '0'.toInt) = true;
}
i += 1;
}
// When all characters unique
print("\n Yes ");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: UniqueCharacters = new UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
import Foundation;
// Swift 4 program for
// Check if a string contains all unique characters
class UniqueCharacters
{
func isUniqueCharacters(_ data: String)
{
let text = Array(data);
let n: Int = text.count;
if (n == 0)
{
// Empty String
return;
}
var characters: [Bool] = Array(repeating: false, count: 255);
print("\n Given text : ", data, terminator: "");
var i: Int = 0;
// Count frequency of character element
while (i < n)
{
if (characters[Int(UnicodeScalar(String(text[i]))!.value) -
Int(UnicodeScalar(String("0"))!.value)] == true)
{
// When characters are repeated
print("\n No ", terminator: "");
return;
}
else
{
characters[Int(UnicodeScalar(String(text[i]))!.value) -
Int(UnicodeScalar(String("0"))!.value)] = true;
}
i += 1;
}
// When all characters unique
print("\n Yes ", terminator: "");
}
}
func main()
{
let task: UniqueCharacters = UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
main();
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
// Kotlin program for
// Check if a string contains all unique characters
class UniqueCharacters
{
fun isUniqueCharacters(text: String): Unit
{
val n: Int = text.length;
if (n == 0)
{
// Empty String
return;
}
val characters: Array < Boolean > = Array(255)
{
false
};
print("\n Given text : " + text);
var i: Int = 0;
// Count frequency of character element
while (i < n)
{
if (characters[text.get(i).toInt() - '0'.toInt()] == true)
{
// When characters are repeated
print("\n No ");
return;
}
else
{
characters[text.get(i).toInt() - '0'.toInt()] = true;
}
i += 1;
}
// When all characters unique
print("\n Yes ");
}
}
fun main(args: Array < String > ): Unit
{
val task: UniqueCharacters = UniqueCharacters();
// Test
task.isUniqueCharacters("xyzklsadfl");
task.isUniqueCharacters("3424789dfkj");
task.isUniqueCharacters("kalicodes");
task.isUniqueCharacters("908");
}
Output
Given text : xyzklsadfl
No
Given text : 3424789dfkj
No
Given text : kalicodes
Yes
Given text : 908
Yes
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment