Personal Website and Computer Science TUR EN

Check Permutation Two Strings

Last update: 10/19/2018 12:14:00 AM

Written by:Fatih KABAKCI

The question is to check if one of the permutation of the strings has already another one in given two strings. Brute force of the solution of this problem would take O(n2 * n!) since there is a calculation of all possible permutations of strings. Therefore, we should not go over this solution. There should be a better implementation of this !

There are only two strings need to be compared to see if one of them is a permutation version of another. What if we would check only frequency of the characters as long as they are same length strings ? In order to do this, we need to know each character how many times they appear. Then, basically two character sets will involve each other by checking their frequencies if they are same or not. First, we have to make sure that if the size of strings are not equal, so we won't have to continue the algorithm, we will just return false because all permutations have to be same length versions.

We could use different type of data structures to solve this problem. We will use Map objects here.

O(n) Solution

Mark each character how many times they appear and store in a map. Then, iterate through the map, and compare all of them match. The solution have been provided in below.

package com.fatihkabakci.arrStr;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

 * @author fkabakci
 * Given two strings, determine if one of them is permutation of the other.
 * O(n) solution
 * abc -- cba -- true
 * abc -- dac -- false
public class CheckPermutation {
	private static Map<Character, Integer> getMap(String str) {
		Map<Character, Integer> m = new HashMap<Character, Integer>();
		for(char key : str.toCharArray()) {
				m.put(key, m.get(key) + 1);
				m.put(key, 1);
		return m;
	public static boolean checkPermutation(String strA, String strB) {
		if(strA.length() != strB.length())
			return false;
		Map<Character, Integer> mapA = getMap(strA);
		Map<Character, Integer> mapB = getMap(strB);
		for(Entry<Character, Integer> e : mapA.entrySet()) {
			if(mapB.get(e.getKey()) == null || e.getValue() != mapB.get(e.getKey()))
				return false;
		return true;
	public static void main(String[] args) {
		System.out.println(checkPermutation("abc", "abc"));

There has been no comment yet


   Please verify the image

The Topics in Computer Science

Search this site for


Software & Algorithms


In mathematics and computer science, an algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing, and automated reasoning.

Programming Languages


A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine. Java,C, C++,C#



A database is an organized collection of data. The data are typically organized to model aspects of reality in a way that supports processes requiring information.



Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web Technologies


Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies. J2EE,Spring Boot, Servlet, JSP,JSF, ASP

Mobile Technologies


Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones. J2ME



A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

Operating Systems


An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system. Linux,Windows

Computer Science


Computer science is the scientific and practical approach to computation and its applications.A computer scientist specializes in the theory of computation and the design of computational systems.