// Program to determine if a given string is unique. #include <iostream> #include <string> using namespace std; bool is_unique(string str) { if (str.length() > 256) // assuming ASCII string (requires 256 bits storage) return false; bool a[256] = { false }; // create an array of 256 elements, initialize to false for(int k = 0; k < str.length(); k++) { int ascii_val = int(str[k]); // get the ascii val of char if (a[ascii_val] == true) // chk the ascii_val index of array a. return false; // if it is true, it means we have already // seen this char, so return false a[ascii_val] = true; // otherwise set it to true to mark it as // a char we have seen already } return true; } int main() { string test0 = ""; string test1 = "Michael"; string test2 = "Jackson"; string test3 = "Hello World"; string test4 = "Australia"; bool a = is_unique(test0); bool b = is_unique(test1); bool c = is_unique(test2); bool d = is_unique(test3); bool e = is_unique(test4); cout << test0 << " is " << a << endl; cout << test1 << " is " << b << endl; cout << test2 << " is " << c << endl; cout << test3 << " is " << d << endl; cout << test4 << " is " << e << endl; return 0; }
In C Plus Plus, C Sharp, Java, PHP, Algorithms, Computer Science and more.... By Gurpreet Singh
Showing posts with label Algorithm. Show all posts
Showing posts with label Algorithm. Show all posts
Tuesday, January 20, 2015
Algorithm to determine if a string has all unique characters
Wednesday, July 3, 2013
Linear or Sequential Search Algorithm | Searching Algorithm
Linear or Sequential Search Algorithm is one of simplest search algorithms but no very efficient. The objective of linear search algorithm is to look through an array of data to find a particular item, x. The item, x, is compared to every item in the array from the first item to any stage where a match is found. If a match is not found then item x is not in the array and the solution is 0.
The Best and Worst Case for the Linear Search Algorithm
The Best Case for the linear search algorithm is when only one comparison is required between x and the array A[1], A[2],...A[n]. This occurs when x is the first term in the array.
The Worst Case occurs when algorithm requires n comparison. This occurs when x = A[n], or when x does not appear in the array.
The Best and Worst Case for the Linear Search Algorithm
The Best Case for the linear search algorithm is when only one comparison is required between x and the array A[1], A[2],...A[n]. This occurs when x is the first term in the array.
The Worst Case occurs when algorithm requires n comparison. This occurs when x = A[n], or when x does not appear in the array.
Tuesday, June 11, 2013
C++ Program for Euclid's GCD Algorithm
Euclidean Algorithm is an efficient method of finding the greatest common divisor. It was named after a Greek mathematician Euclid. The Algorithm begins with two positive integers A and B then replaced by X and Y. Then while Y is not zero, at each step X is replaced by Y and then Y is replaced by X mod Y (remainder of a divided by b). The algorithm halts when B = 0.
Pseudo Code for Euclid's GCD Algorithm
Procedure GCD(A, B: positive integers)
X := A
Y := B
while Y != 0
R := X mod Y
X := Y
Y := R
return X {GCD(a,b) is X}
C Plus Plus Program for Euclid's GCD Algorithm
Pseudo Code for Euclid's GCD Algorithm
Procedure GCD(A, B: positive integers)
X := A
Y := B
while Y != 0
R := X mod Y
X := Y
Y := R
return X {GCD(a,b) is X}
C Plus Plus Program for Euclid's GCD Algorithm
Subscribe to:
Posts (Atom)